我试图重定向一个shell命令的输出,就像这样:使用PHP的exec重定向shell命令的输出()函数
# echo here > /tmp/output
我需要从一个PHP脚本中做到这一点正在执行通过使用exec()函数的apache:
var_dump(exec('echo here > /tmp/output')); // string (0) "", no file created
上述命令不与0
退出代码我可以验证PHP是能够执行使用EXEC命令创建文件并返回像这样:
var_dump(exec('echo here')); // string(4) "here"
我试图从运行的Apache用户一个PHP外壳内执行这个代码,它的工作原理:
# su -s /bin/bash -c 'php -a' apache
php > var_dump(exec('echo here > /tmp/output')); // file is created
我试图重定向标准错误和标准输出:
var_dump(exec('echo here 2>&1 /tmp/output')); // string(0) "", no file created
最后,我试图从整个命令重定向stderr设回给exec():
var_dump(exec('echo here > /tmp/output 2>&1')); // string(0) "", no file created
基于上述步骤,在我看来,这个问题必须与调用PHP的apache相关,因为它可以正常运行,从apache用户的php shell运行。
任何帮助非常感谢!
https://stackoverflow.com/questions/3099617/php-capturing-the-command-output – daidoji70
你试图保持输出吗? – rtfm
没有输出是无关紧要的。真正的目标是使用mysqldump写入/ tmp中的文件,这是失败的。回声只是一个简化的测试案例。 – Jon