PHP exec() will not execute shell command when executed via browser

65,099

Solution 1

Add 2>&1 to the end of your command to redirect errors from stderr to stdout. This should make it clear what's going wrong.

Solution 2

Just some guess, it might be that your webserver process user does not have privileges to do so.

Solution 3

Since it works when from the command-line (which would be under your own user account), it sounds to me like the account the web server is running under (often "www-data") does not have execute permissions on the conversion program.

Solution 4

Have you considered file permissions? In the browser, php is running under one user, but when you run it in bash, it is likely running with your user permissions.

It's the first thing I would check.

Amy

Solution 5

Does your Apache/webserver user have the necessary rights to run the shell command?

When you run from the cl you are likely running as a different user, which may explain which cl works but via browser doesn't.

Share:
65,099
user6026401
Author by

user6026401

Updated on April 10, 2020

Comments

  • user6026401
    user6026401 about 4 years

    I have a certain PHP script that calls exec() to execute a command to convert a PDF to JPG. This command works fine in bash.

    To preempt your initial troubleshooting guesses, note the following:

    • safe_mode = Off
    • Permission on the directory containing the PDF and the script is set to 777, and this directory is also where the JPG is being written.
    • The command I am passing to exec() explicitly points to the binary being used (e.g. /usr/local/bin/convert).
    • display_errors = On
    • error_reporting = E_ALL
    • disable_functions = [blank]
    • I am echoing exec()'s output and it returns nothing. The command being run by default returns nothing.

    When I call this PHP script from the browser (visiting http://www.example.com/script.php), exec() does not execute its argument.

    IMPORTANT: I know that there are no issues with my script or the way I have constructed the bash command, because from bash, I can execute the script with 'php' and it works (e.g. 'php script.php' converts the file)

    I have also tried switching out exec() with system().

    Last, I have had this issue once before in the past but cannot remember how I fixed it.

    I know there is something I am missing, so I hope someone else has experienced this as I have and remembers how to fix it!

    Thank you in advance for any assistance you can provide.

    Alex