PHP脚本中chdir调用shell脚本的问题

时间:2015-10-06 20:09:07

标签: php linux bash

我正在尝试使用PHP脚本来调用shell脚本并将输出打印到浏览器。

我已确认所有权限设置都已正确设置,PHP和bash脚本都具有完全控制权。

如果我在当前目录(与PHP文件相同的目录)中运行shell脚本,那么它可以正常工作。但是如果我尝试从/some/other/directory/shell.script运行shell脚本,那么它就不起作用了。为什么会这样?

我已尝试将chdir()发送到另一个目录,但在运行getcwd()之后,它从未更改过目录。

我自己也尝试了exec命令,但它不起作用。

<?php error_reporting(E_ALL); ini_set('display_errors',1);

echo "<h1>EDS Count Report</h1><p>";

$output=shell_exec('cd /app/script/catalog/ && ./EDScount_report.sh');
echo $output;
?>

SHELL脚本的内容:

#!/bin/bash echo "this is a test!"

1 个答案:

答案 0 :(得分:1)

请试试这个

<?php
$output=shell_exec('sh /path/to/otherdirectory/shellscript.sh');
echo $output;
?>

如果您确定可以访问该dicertory,您也可以使用:

<?php
    $output=shell_exec('cd /path/to/otherdirectory/ && ./shellscript.sh');
    echo $output;
    ?>