简体   繁体   中英

PHP EXIF data not working

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);

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

I spent weeks with this problem, so in the end I decided to create a library to extract the metadata (Exif, XMP, GPS...) from a PNG in PHP, 100% native, I hope it helps. :) PNGMetadata

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM