Search code examples
linuxperlbashshellexiftool

Cannot run shell command through php exec, but can as user on shell?


I'm trying to get exiftool to work on my dedicated server. The issue is that PHP exec seems to run different than when a command is run as a user. Oddly enough, PHP shows up as the same user I log in with, but it does not behave the same with system commands.

Oddly enough everything works great on my localhost, but not on my server.

So as mentioned, running exiftool commands logged in via ssh is fine.

But running in a php testing script (note I've installed exiftool on each tested directory, and it runs through ssh), nothing is accessible, though it runs as user orangeman...

And it fails

Here is an update - having been on this all day:

On the shell:

-bash-4.1$ which exiftool -a
~/perl5/bin/exiftool
/usr/bin/exiftool
~/perl5/bin/exiftool

In PHP shell_exec('exiftool -a');

/usr/bin/exiftool

And here is what that file links to:

lrwxrwxrwx    1 root root          33 May 15 02:10 exiftool -> /home/orangeman/perl5/bin/exiftool

I've also tried creating symlinks of various sorts, tampering with the main $PATH variable via putenv(); in php ... I'm truly in the dark here. Works on localhost, not on dedicated server.


I've updated this with a bounty - its a serious issue in development.

I'm on a dedicated server, and the problem is as outlined above.


UPDATE Per @gcb suggestion, I was able to print out the error that is occurring when php's exec() function runs the system command with no effect.

PHP

<?php
exec('exiftool 2>&1', $output, $r);
var_dump($output, $r);
?>

Output:

array(2) {
  [0]=>
  string(230) "Can't locate Image/ExifTool.pm in @INC (@INC contains: /bin/lib /usr/local/lib64/perl5 /usr/local/share/perl5 /usr/lib64/perl5/vendor_perl /usr/share/perl5/vendor_perl /usr/lib64/perl5 /usr/share/perl5 .) at /bin/exiftool line 33."
  [1]=>
  string(59) "BEGIN failed--compilation aborted at /bin/exiftool line 33."
}

UPDATE

@gcb's solution worked. Thank you very much.


Solution

  • So you do not have a php problem now, but a perl one. your include path is bad.

    Answer here.

    You either have to install the ExifTool libraries in the
    standard location (ie. somewhere in the @INC directories
    listed in your post), or add the location to the include path,
    something like this:
    
    Code:
    #!/usr/bin/perl
    BEGIN { unshift @INC, "PATH_TO_DIRECTORY_CONTAINING_LIBRARIES" }
    use Image::ExifTool;
    
    You should be able to add "Image/ExifTool.pm" to the path you add
    to find the ExifTool module.
    
    - Phil
    

    I still think using my suggestion #3 from the previous answer will fix it. If not and you really want to know the reason, create a new perl script that just outputs the contents of @INC and run it via the shell and via php. you will see the difference, then you need to find which login script is not being honored in php and open a bug against php for shell_exec not respecting it...

    though the easier solution for your problem (as it does not look like you are too interested in explanations) is to just set the PERLLIB var before calling the script.

    So, just do:

    1. find / -name ExifTool.pm This will tell you where the lib is installed. let's say this returns /example/perl/Image/ExifTool.pm
    2. append PERL5LIB=/example/perl/ to your exec() call.
    3. exec("PERL5LIB=/example/perl/ /var/www/myscript/execscript.sh {$param}"); #and