Get Off My Lawn
Get Off My Lawn

Reputation: 36311

PHP Script works in the terminal but not the browser

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;

Here is the file shell_exec is calling:

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

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

Upvotes: 2

Views: 4885

Answers (2)

Brad
Brad

Reputation: 163232

The issue likely has to do with permissions.

When you run something from the shell, it runs under your user account. The web server will be running as something differently, generally with very few permissions to anything. Either give the web server's account the appropriate permissions, or run the server under a different account (not recommended!).

Upvotes: 2

mcryan
mcryan

Reputation: 1576

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.

Upvotes: 5

Related Questions