PHP脚本可在终端中使用,但不能在浏览器中使用 [英] PHP Script works in the terminal but not the browser

查看:134
本文介绍了PHP脚本可在终端中使用,但不能在浏览器中使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试执行exec命令,但遇到了问题.当我运行以下代码时,通过浏览器运行它不起作用.但是,如果我将$str复制的输出粘贴到终端中,它就可以正常工作.是什么原因造成的,我该如何解决?目前,我正在localhost上运行.

I am trying to execute a exec command, and I am having problems. When I run the following code, it doesn't work when I run it through the browser. but if I take the output of $str copy and paste it into a terminal it works just fine. What would be causing this and how can I fix it? Currently I am running on localhost.

$php_location = "/usr/bin/php";
$data = str_replace("\"", "\\\"", json_encode($_POST));
$cwd = __DIR__;

shell_exec($str = "$php_locataion $cwd/phpExcel.php \"$data\" > /home/ryannaddy/Desktop/phpExcel.txt &");

echo $str;

这是shell_exec正在调用的文件:

<?php
set_time_limit(0);
$args = $argv[1];

$data = json_decode(str_replace("\\\"", "\"", $args), true);
echo "hello!";

推荐答案

通过浏览器运行它时,它将作为Web服务器的用户执行,该用户可能没有正确的权限.当您在终端中运行它时,它将以您登录时所使用的任何用户身份执行.检查apache(假设您正在使用apache)用户对脚本/目录具有正确的权限.

When you run it through the browser, it executes as the user for the web server who may not have the correct permissions. When you run it in the terminal it executes as whatever user you are logged in as. Check that the apache (assuming you are using apache) user has the correct permissions to the script / directory.

这篇关于PHP脚本可在终端中使用,但不能在浏览器中使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆