简体   繁体   中英

Running exec() from PHP not working

This works within my PHP code

$output = array();
exec("ls /Applications/XAMPP/htdocs/MY_APP/images", $output);
var_dump($output);

Now I need to use ImageMagick's "convert" command to convert a PNG file to PDF file. But the following doesn't do anything and returns no errors.

$output = array()
exec("convert /Applications/XAMPP/htdocs/MY_APP/images/test.png /Applications/XAMPP/htdocs/MY_APP/images/test.pdf", $output);
var_dump($output);

Is it a permission issue? I gave chmod 777 to the images folder. What else should I check? When I run the command from the terminal, it works fine.

Try changing convert to the full path to convert eg usr/local/bin/convert

This can be found with:

<?php
echo "<pre>";
system("type convert"); 
echo "</pre>";
?>

or

<?php
echo "<pre>";
system('which convert',$path); print_r($path); 
echo "</pre>";
?> 

I have just reread your post and notice you are on XAMPP and so the answer above may not work.

Try putting everything in the same folder to cut out any problems with paths etc.

exec("convert test.png test.pdf");

Also do you have ghostscript installed? To prove it works and not a ghostscript problem try saving as a jpg instead of a pdf.

If your PHP code is being executed via you web server (eg Apache) then the Apache process (httpd) might be running under a restricted unix user (eg apache or httpd). That restricted unix user wouldn't usually have write permissions in /Applications/XAMPP/htdocs/MY_APP/images/ directory where you're trying to generate pdf. Try this command instead and see if this works:

exec("convert /Applications/XAMPP/htdocs/MY_APP/images/test.png /tmp/test.pdf", $output);
var_dump($output);

Above command tries to generate pdf in /tmp directory which usually is writable for Apache unix user.

I note that you've given 777 to images folder. Pls understand that chmod 777 to images folder alone is not enough. You need to give write permission to Apache user in all parent directories as well eg MY_APP, htdocs, XAMPP, Applications etc which is a big security risk , I must add.

I have had the same problem: safe mode was off, but still I couldn't execute Image Magick's convert.

I found out that, for some reason, php can't see the script (even including the path in include_path through php.ini). Checking the log, I found that the error was:

sh: convert: command not found

Trying to call the script using the fullpath (I checked it using which convert ) I finally managed to make it run normally through exec .

So, supposing the convert fullpath is /usr/local/bin , try running the following:

<?php    
    $output = array();
    exec("/usr/local/bin/convert /Applications/XAMPP/htdocs/MY_APP/images/test.png /Applications/XAMPP/htdocs/MY_APP/images/test.pdf", $output);
    var_dump($output);
?>

If you have this problem under Windows, try exec('identify pathtoyourfile'). If that works but exec('convert ...') does not you are probably inadvertently calling Window's convert utility instead of IM's convert. The solution is to rename IM's convert to, say, cconvert. Alternatively, check if IM is at the beginning of your PATH environment variable.

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