0

I want to get comment metadata of images inside ../banners folder

$arr = scandir("../banners/");
foreach($arr as $el){
  echo $el;  // this works
  $path = '../banners/' . $el;
  //$arrb = exif_read_data($path, 0, true); // line 91
  $arrb = exif_read_data($path, "COMMENT");  // line 92
  print_r($arrb);
}

Result: If I execute line 91 everything is listed, but anyway - there is a warning:

Warning: exif_read_data(): Unable to open file... on line 91

If I execute line 92 there is no data but the warning is there:

Warning: exif_read_data(): Unable to open file... on line 92

I'm on localhost, windows 7, wamp.

Any help?

  • Permissions maybe? Or maybe absolute/relative paths? – brombeer May 26 '18 at 11:00
  • @kerbholz, how to check permissions? –  May 26 '18 at 11:03
  • Talking about file permissions here, depends on the OS you're using, on Linux I usually just do a `ls -l` which results in something like `-rw-r--r-- 1 usr user 4 May 26 10:04 composer.json` with `-rw-r--r--` being the permissions – brombeer May 26 '18 at 11:06
  • @kerbholz, I'm on windows 7, localhost, wamp. –  May 26 '18 at 11:09
  • You could also check some of the examples on PHPs [exif_read_data](https://secure.php.net/manual/en/function.exif-read-data.php) page to see if they work for you. – brombeer May 26 '18 at 11:10
  • No wait, you're using `scandir` which also returns the current directory `.` and the parent directory `..`. In your `foreach` you should filter those out. Or use [glob()](https://secure.php.net/manual/en/function.glob.php) instead of `scandir`. – brombeer May 26 '18 at 11:13
  • @kerbholz, see my update, pls. –  May 26 '18 at 11:17

1 Answers1

0

scandir() will return the current directory . and the parent directory ... You want to filter those out:

<?php
$blacklist = ['.', '..'];
$arr = scandir("../banners/");
foreach ($arr as $el) {
    if (!in_array($el, $blacklist)) {
        echo $el;  // this works
        $path = '../banners/' . $el;
        $arrb = exif_read_data($path, "COMMENT");  // line 92
        print_r($arrb);
    }
}

I created a $blacklist array that holds those values you don't want to be interpreted as images.

Another way could be to use glob() instead:

<?php
foreach (glob('../banners/*.jpg') as $el) {
    echo $el;  // this works
    $path = '../banners/' . $el;
    $arrb = exif_read_data($path, "COMMENT");  // line 92
    print_r($arrb);
}

This will return only *.jpg without you having to filter out unwanted filetypes/directories.

brombeer
  • 8,716
  • 5
  • 21
  • 27
  • I tried, there is no warning, but there is no data also –  May 26 '18 at 11:23
  • Enable error reporting, make sure the file actually has an EXIF comment, view PHP error log etc. – brombeer May 26 '18 at 11:28
  • error reporting is `ON`. Maybe the problem is because I wrote the comments to images (right click on image - details...). This is not comment wiritten by device. But in that case `line 91` should not work also. –  May 26 '18 at 11:32
  • I used GIMP (on Linux though) to edit/add a comment to an image and the code above worked fine. – brombeer May 26 '18 at 12:51