PHP current() expects parameter 1 to be array, null given on json_decode error

Started by xberg, January 25, 2016, 05:03:36 AM

Previous topic - Next topic

xberg

Hi,

I am sometimes getting a PHP error:
E_WARNING: current() expects parameter 1 to be array, null given

On the json_decode:

$array = shell_exec('/usr/bin/exiftool -j '.$filename.'');
if (!empty($array)){
$array = (array) current(json_decode($array));
}


Any thoughts as to what I should do to avoid this error?

Thanks!

Hayo Baan

What is the output of the exiftool command? My guess is that you need to add some error handling.
Hayo Baan – Photography
Web: www.hayobaan.nl

xberg

I'm not aware of the exiftool output. Should I be logging output?
Users upload images and I run them thru exiftool. Most of the time if works, but sometimes I get the error mentionned above...

Hayo Baan

Well, the PHP snippet you included, certainly grabs the output of exiftool shell_exec('/usr/bin/exiftool -j '.$filename.'');. It is the output of that command that makes your PHP fail. So what is the output for the file that gives you the error? Then you should be able to see what needs to be changed in your PHP.
Hayo Baan – Photography
Web: www.hayobaan.nl

krzysiu

Sorry, I don't want to offend you, but to be honest you are lacking basics and you aren't reading manuals. When you'll learn about basic commands, you should learn important thing - debugging. Like other people said, we need to know what happens between commands. If you'd use var_dump(json_decode($array)); you'd have an answer why it's not gonna work.

But I wonder - how comes sometimes it give error? In my opinion it should always show error. json_decode($foo) returns object, not array, and you are passing that object into array function. And why would you get value of element of array pointed by internal pointer and convert is to array? That's something very weird.

And why are you checking it by empty()? If exiftool will fail and show error message, empty() expression will evaluate to true. If that's supposed to catch errors, you should use exec() and read exit code, but well, that's the thing that's written in manual: "It is not possible to detect execution failures using this function. exec() should be used when access to the program exit code is required."

To use array functions you need to have array. To convert string to array you need to use json_decode($foo, true). Again - manual. The second parameter is "When TRUE, returned objects will be converted into associative arrays".


<?php
$filename escapeshellarg($filename); // you need to escape file name!
$cmdOutput null// initialize var, because we will send pointer, so it should exist already
@$cmd exec("/usr/bin/exiftool -j {$filename}"$cmdOutput$exitCode); // use exec, not shell_exec; the line is silenced, as we have own error handling
if ($exitCode === 0) { // "The exiftool application exits with a status of 0 on success" - from ExifTool manual
$cmdOutput implode(PHP_EOL$cmdOutput); // exec returns array exploded by PHP_EOL, so we reverse it;
@$json json_decode($cmdOutputtrue); // to get array you need to use second parameter! Silenced for the same reason.
if (json_last_error() === JSON_ERROR_NONE) { // check if json decoding passed
// here's the code if command passed _and_ JSON decode passed
} else echo json_last_error_msg(); // show last error, if occured
} else echo "Command failed. The exit code: {$exitCode}." . ($cmd "The last line of output: {$cmd}''); // error handler for exec; exit code and (if there's anything) the last line of output
"We would use teleporters and live on another planets, if only ExifTool would be present when I was researching cosmos and physics"
Albert Einstein