我想知道我们是否可以从浏览器中的html页面创建文件到linux服务器文件夹?我有一个shell脚本,可以创建txt文件。。但是当我使用ajax从浏览器触发这个脚本时,它不会创建任何文件。。我给了父目录所有的权限,但我仍然没有看到文件创建,也没有任何错误。
test.sh
echo "first sh"
java HelloWorld
echo "t1 test" > t1s
if [ ! -f t1s ]
then
echo "Shell File is not created .."
else
echo "Shell file is created.. "
fi
------
first.php
<?php
$output=shell_exec('sh test.sh');
echo "Hello Php 'r'n";
echo $output;
echo "done 'r'n";
?>
-----------
This is the output from command line:
Hello Php
hello first sh Shell file is created..
done
----------------
This is the output from broswer:
Hello Php hello first sh Shell File is not created .. done
通过web服务器执行脚本时,请注意web服务器使用的环境很少与CLI用户看到的环境完全相同。例如,命令
sh test.sh
做了两个假设:sh
在$PATH中,test.sh
在当前工作目录中。当您的Web服务器尝试运行该命令时,这两种情况很可能都不正确。
为了获得更好的结果,请尝试同时包含命令和文件的完整路径。例如:
shell_exec('/bin/sh /var/www/html/scripts/test.sh');
最后,检查test.sh
上的可执行权限,以确保web用户(httpd、apache或其他)有权执行该脚本。