Shell命令'touch'在PHP中不起作用

问题描述 投票:2回答:2

我在redhat linux下面有php脚本(test.php)。

<?php
shell_exec('touch /var/www/html/test.txt');
?>

如果我在命令行(php test.php)中运行此脚本,它将按预期工作。但是如果我在浏览器(http://hostname/test.php)中运行test.php,它就不会创建文件test.txt。

我尝试编辑sudo visudo www-data ALL =(ALL)NOPASSWD:ALL

请帮我这个!!

php shell
2个回答
0
投票

我是访客,因为许可,当您在命令行中运行时,您以用户身份运行它,但是当您通过浏览器运行它时,它作为Web服务的用户运行,因此请检查/var/www/html/目录的权限并将其权限设置为755和将所有者更改为您的Web服务(apache,apache2等您作为web服务)

所以

chown -R apache:apache /var/www/html/ (I'm not sure about your web-service, change it your webserice and it's group)
chmod -R 755  /var/www/html/

0
投票

PHP会执行就好了。但是系统在它必须搜索的路径中找不到touch

最简单的,是给touch的完整路径。在我的系统上使用命令whereis查找touch

$whereis touch
touch: /usr/bin/touch /bin/touch /usr/share/man/man1/touch.1.gz

所以脚本将是:

<?php
shell_exec('/usr/bin/touch /var/www/html/test.txt');
?>
© www.soinside.com 2019 - 2024. All rights reserved.