PHP EXIF data not working

2019-07-08 03:03发布

I am new to PHP, and adapting a script from http://www.techrepublic.com/article/create-a-dynamic-photo-gallery-with-php-in-three-steps/ which generates a table of images in a directory along with some accompanying EXIF data. The only problem is that the code has not display the EXIF data. This happens with even the original source code. My best guess of what is happening is that something in the original source code is old and outdated, and no longer supported by modern PHP. I have made sure that my server has EXIF enabled.

Here's the code:

<table>
<?php
// define directory path
$dir = "path/to/directory";

// iterate through files
// look for JPEGs
if (is_dir($dir)) {
  if ($dh = opendir($dir)) {
    while (($file = readdir($dh)) !== false) {
      if (preg_match("/.jpg/", $file)) {

        // read EXIF headers
        $exif = exif_read_data($file, 0, true);

        // get image
        echo "<tr><td rowspan='3'><img src='$dir/$file'></td>";

        // get file name
        echo "<th>Title</th><td>" . $exif['FILE']['FileName'] . "</td></tr>";

        // get timestamp
        echo "<tr><th>Year</th><td>"  . $exif['IFD0']['DateTime'] . "</td></tr>";

        // get image dimensions
        echo "<tr><th>Description</th><td>" . $exif['IFD0']['Comments'] . "</td></tr>";

      }
    }
    closedir($dh);
  }
}
?>
</table>

EDIT: I also get the following error logs:

20160815T185355: benxd.me/art/gallery.php 
PHP Warning:  exif_read_data(): Unable to open file in /hermes/walnaweb01a/b893/pow.hdemoras/htdocs/benxd/art/gallery.php on line 21 
PHP Warning:  exif_read_data(): Unable to open file in /hermes/walnaweb01a/b893/pow.hdemoras/htdocs/benxd/art/gallery.php on line 21 
PHP Warning:  exif_read_data(): Unable to open file in /hermes/walnaweb01a/b893/pow.hdemoras/htdocs/benxd/art/gallery.php on line 21 
PHP Warning:  exif_read_data(): Unable to open file in /hermes/walnaweb01a/b893/pow.hdemoras/htdocs/benxd/art/gal 

In my code line 21 is $exif = exif_read_data($file, 0, true);

1条回答
Animai°情兽
2楼-- · 2019-07-08 03:18

Try explicitly adding the full path and the list of sections:

$exif = exif_read_data($dir . $file, "FILE,COMPUTED,ANY_TAG,IFD0,THUMBNAIL,COMMENT,EXIF", true);

Source: http://php.net/manual/en/function.exif-read-data.php

查看更多
登录 后发表回答