2

I am trying to use exiftool (because it has all image tags vs. other tools) on a webserver. The perl module is installed in /bin/exiftool

The test.jpg is the same directory and has EXIF data.

<?php
#!/usr/bin/exiftool

$array = [];

eval(`$array=` . `exiftool -php -q test.jpg`);

print_r($array);
?>

The $array is empty though. I also tried:

eval(`$array=` . `exiftool -php -q test.jpg > output.txt`);

This creates an empty output.txt file.

I feel I have something very basic wrong but can't work it out. Like to add, I am on a shared host and have root/command line access.


No Update: I still struggle. I think it's related to cgi / perl (or my lack of knowledge about it)

yello
  • 191
  • 1
  • 13
  • 2
    On a shared host, is `eval()` enabled for your use? It is commonly disabled. Any error output in your error log? To be honest, I would not use the `eval()` approach with the php output array. Instead, I would probably use `\`exiftool -json...\`` for JSON output then load it with `json_decode()`. – Michael Berkowski Nov 09 '19 at 03:25
  • When executing a shell command with backticks in PHP, it is also advisable to use the full path `/bin/exiftool`, as you cannot rely on it to have a sane `$PATH`. – Michael Berkowski Nov 09 '19 at 03:27
  • @Michael, `eval()` works, I tried with: `eval("echo 'Hello World!';");`. I know, json is more elegant, but for now I am only on a test server. Adding the path did not make it work. – yello Nov 09 '19 at 05:42
  • Not so much that `eval()` is inelegant, but more that when you are not in control of both ends of the code generation and consumption, it introduces risks. (exiftool is probably trustworthy) – Michael Berkowski Nov 09 '19 at 14:34
  • Thank you, my question wasn't really about `eval()` though. – yello Nov 11 '19 at 12:15

2 Answers2

2

You can use the exec() function for that:

$exifToolPath = 'cgi-bin/exiftool';
$exifToolParams = '-G';

// Read EXIF from jpg
$img = 'test.jpg';
exec($exifToolPath.' '.$exifToolParams.' '.$img, $data);
var_dump(array_unique($data));

// Read EXIF from NEF
$img = 'test.NEF';
exec($exifToolPath.' '.$exifToolParams.' '.$img, $data);

// Read XMP
$img = 'test.xmp';
exec($exifToolPath.' '.$exifToolParams.' '.$img, $data);
sort($data);
var_dump(array_unique($data));

You can also install exiftool with composer.

Simon
  • 324
  • 1
  • 13
1

As far as I know, exiftool is a Perl program and thus external code to PHP. I think eval is not probate to run external code. Try exec.

Datahardy.

Obsidian
  • 3,719
  • 8
  • 17
  • 30
datahardy
  • 11
  • 1