This was working in 7.X version however upon upgrading to 8.0 the page no longer loads. Have tried displaying errors but it breaks. The script is loading images from a specified folder.

<? 
ini_set('display_errors', '1');
ini_set('display_startup_errors', '1');
error_reporting(E_ALL);
// image file types to display
  $imagetypes = ['image/jpeg', 'image/gif', 'image/png'];

  function getImages($dir)
  {
    // array to hold return value
    $retval = [];

// add trailing slash if missing
     $dir .= "/";

// full server path to directory
$fulldir = "/path-to-uploads/uploads/";

$d = @dir($fulldir) or die("getImages: Failed opening directory {$dir} for reading");
while(FALSE !== ($entry = $d->read())) {
  // skip hidden files
  if($entry{0} == ".") continue;

  // check for image files
  $f = escapeshellarg("{$fulldir}{$entry}");
  $mimetype = trim(shell_exec("file -bi {$f}"));
  foreach($GLOBALS['imagetypes'] as $valid_type) {
    if(preg_match("@^{$valid_type}@", $mimetype)) {
      $retval[] = [
       'file' => "/{$dir}{$entry}",
       'size' => getimagesize("{$fulldir}{$entry}")
      ];
      break;
    }
  }
}
$d->close();

return $retval;
  }

  // fetch image details
  $images = getImages("uploads");

  // display on page
  foreach($images as $img) {
//str_replace('images', '', $img['file']);
echo "<div class=\"photo\">";
echo "<form method='post' action='reviewfiles.php'>";
  echo "<input type='hidden' name='file_name' value='".$img['file']."'>";

echo "<img width=\"200px\" height=\"100%\" src=\"{$img['file']}\" {$img['size'][3]} alt=\"\">\n";

  echo "<br><input type='submit' name='delete_file' value='Delete File'>";
  echo "</form></div>";

  }?>

Does it work if you change the opening tag to <?php instead of the short form tag? If so, I'd personally recommend using the full tag going forward for maximum portability, but if there's a reason you need to use the short form, then check your short_open_tag setting.

If that's not it, maybe remove the @ before the call of the dir() function, in case that's not working now for some reason and you are suppressing the error message.

    Hm, didn't seem to do anything. Changed to <?php and removed the '@' symbol before dir.

      crawdidly if($entry{0} == ".") continue;

      I you had the php error related settings in the php.ini on your system, you would be getting a fatal error about - Array and string offset access syntax with curly braces is no longer supported ... in that line of code.

      Change the {}'s to []'s.

      For this type of error, your code never runs to cause the lines of code setting php's error related settings to take effect. Php error settings should always be in the php.ini on your system so that they can be changed at a single point. Also, you cannot set display_startup_errors in your code, since php has already been started at that point.

        I didn't realize that was depricated in 8.0, thanks that fixed it.

        Write a Reply...