简体   繁体   English

PHP exec 命令适用于 shell 和 php 命令行,但不适用于网站

[英]PHP exec command works in shell and php command line but not on website

I have a problem where I am trying to copy a file that is generated on a server drive that I have mounted using the php exec command.我在尝试复制使用 php exec 命令安装的服务器驱动器上生成的文件时遇到问题。 However, the command doesn't work (although the return status is 1) when called from a web-page.但是,当从网页调用时,该命令不起作用(尽管返回状态为 1)。

$src = "/mnt/...";
$dest = "/var/www/...";
exec("cp $src $dest");

I have tried printing out the command to make sure it is correct, and it is.我已经尝试打印出命令以确保它是正确的,并且确实如此。 I have also tried making sure the file exists before attempting to copy it and it is.我还尝试在尝试复制文件之前确保该文件存在,并且确实存在。

if (file_exists($src)) {
    exec("cp $src $dest");
}

Copying the command directly into the terminal works.将命令直接复制到终端可以工作。

$ >cp /mnt/... /var/www/...

I've also tried using the php command line tool to run the exec command and that also works.我也试过使用 php 命令行工具来运行 exec 命令,这也有效。

$ >php -r 'exec("cp /mnt/... /var/www/...");'

I have also tried using shell_exec as well, with the same results.我也尝试过使用shell_exec ,结果相同。

Hostings usually disable the shell commands.主机通常禁用 shell 命令。 On your local, you can edit your php.ini and add this line to disable the functions you want:在你的本地,你可以编辑你的php.ini并添加这一行来禁用你想要的功能:

disable_functions =exec,passthru,shell_exec,system,proc_open,popen,curl_exec,curl_multi_exec,parse_ini_file,show_source

But on your live website, you need to contact your host provider to enable the functions that are disabled by default.但是在您的实时网站上,您需要联系您的主机提供商以启用默认禁用的功能。

You can add the second parameter to help debug, $output will display what the cp command is doing, whether it be an error or not.您可以添加第二个参数来帮助调试, $output将显示cp命令正在执行的操作,无论是否出错。

I would also recommend placing quotes around the files, just in case something with a space gets in there.我还建议在文件周围加上引号,以防万一里面有空格。

$src = "/mnt/...";
$dest = "/var/www/...";
exec("cp '$src' '$dest'", $output, $retrun_var);

var_dump($output, $retrun_var);

I had a similar issue a while ago.不久前我遇到了类似的问题 It was essentially what all the commentators are saying.这基本上就是所有评论员所说的。 The user/permission of the web server are restricted, but also the shell environment that is being used and/or the PATH environment variable. Web 服务器的用户/权限受到限制,而且正在使用的 shell 环境和/或 PATH 环境变量也受到限制。

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

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