简体   繁体   English

从PHP运行exec()无法正常工作

[英]Running exec() from PHP not working

This works within my PHP code 这适用于我的PHP代码

$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. 现在,我需要使用ImageMagick的“转换”命令将PNG文件转换为PDF文件。 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. 我将chmod 777放入了images文件夹。 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 尝试将convert更改为完整路径以进行转换,例如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. 我刚刚重读了您的帖子,并注意到您正在使用XAMPP,因此上述答案可能无效。

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? 您还安装了ghostscript吗? To prove it works and not a ghostscript problem try saving as a jpg instead of a pdf. 为了证明它有效,而不是ghostscript问题,请尝试另存为jpg而不是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). 如果您的PHP代码是通过Web服务器(例如Apache)执行的,则Apache进程(httpd)可能会在受限制的unix用户(例如apache或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. 受限制的Unix用户通常在试图生成pdf的/Applications/XAMPP/htdocs/MY_APP/images/目录中没有写权限。 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. 上面的命令尝试在/tmp目录中生成pdf,这通常对于Apache unix用户是可写的。

I note that you've given 777 to images folder. 我注意到您已经为图像文件夹指定了777。 Pls understand that chmod 777 to images folder alone is not enough. 请了解仅chmod 777到images文件夹是不够的。 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. 您还需要在所有父目录中向Apache用户授予写权限,例如MY_APP,htdocs,XAMPP,Applications等,这是一个很大的安全隐患 ,我必须补充。

I have had the same problem: safe mode was off, but still I couldn't execute Image Magick's convert. 我遇到了同样的问题:安全模式已关闭,但仍然无法执行Image Magick的转换。

I found out that, for some reason, php can't see the script (even including the path in include_path through php.ini). 我发现由于某种原因,php无法看到脚本(甚至包括通过php.ini包含在include_path中的路径)。 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 . 尝试使用fullpath调用脚本(我使用which convert对其进行了检查),我最终设法使其通过exec正常运行。

So, supposing the convert fullpath is /usr/local/bin , try running the following: 因此,假设转换全路径为/usr/local/bin ,请尝试运行以下命令:

<?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'). 如果您在Windows下遇到此问题,请尝试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. 如果可以,但是exec('convert ...')不起作用,您可能无意中调用了Window的convert实用程序而不是IM的convert。 The solution is to rename IM's convert to, say, cconvert. 解决方案是将IM的转换重命名为cconvert。 Alternatively, check if IM is at the beginning of your PATH environment variable. 或者,检查IM是否位于PATH环境变量的开头。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM