34

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

Paige Ruten
  • 157,734
  • 36
  • 172
  • 191
Alex
  • 367
  • 1
  • 4
  • 9
  • In PHP 5 make sure to use E_ALL | E_STRICT for complete error reporting. – Ross Feb 12 '09 at 18:33
  • *UPDATE -- IMPORTANT*: I found that this was an issue with ImageMagick, and accomplished the PDF to JPEG conversion with GhostScript (with a much longer command) instead of using ImageMagick as a middle-man. There were no issues with PHP, my permissions, or exec(). Thank you For all of your input! – Alex Feb 12 '09 at 21:26
  • What are the arguments being passed to convert? Do they include the full path to the file? – Powerlord Feb 11 '09 at 21:47

9 Answers9

69

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.

Greg
  • 295,929
  • 52
  • 357
  • 326
  • Yep, I'm getting a strange ImageMagick error: convert: missing an image filename `test.jpg' @ convert.c/ConvertImageCommand/2766. After searching around a bit with Google, it looks like it could be a buggy release. I'm rebuilding the latest stable revision and testing in a few minutes. – Alex Feb 12 '09 at 17:57
  • 1
    what does the 2>&1 mean? and what is that called? – Patoshi パトシ Aug 26 '15 at 17:22
6

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

TomHastjarjanto
  • 5,431
  • 1
  • 27
  • 42
  • Excellent! This is a very good point. If calling a bash script with exec (through php) then make sure the bash script is readable by Apache too. – mr-euro Nov 16 '10 at 16:34
5

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.

Chad Birch
  • 69,230
  • 22
  • 145
  • 148
3

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

Amy Anuszewski
  • 1,803
  • 17
  • 28
2

The Apache’s user www-data need to be granted privileges to execute certain applications using sudo.

  1. Run the command sudo visudo. Actually we want to edit the file in etc/sudoers.To do that, by using sudo visudo in terminal ,it duplicate(temp) sudoers file to edit.
  2. At the end of the file, add the following ex:-if we want to use command for restart smokeping and mount command for another action,

www-data ALL=NOPASSWD: /etc/init.d/smokeping/restart, /bin/mount

(This is assuming that you wish to run restart and mount commands using super user (root) privileges.)

However, if you wish to run every application using super user privileges, then add the following instead of what’s above.You might not want to do that, not for ALL commands, very dangerous.

www-data ALL=NOPASSWD: ALL

3.After edit the sudoers file(by visudo we edit the temp file of sudoers so save and quit temp file(visudo) to write in sudoers file.(wq!)

4.That’s it, now use exec() in the following manner inside your xxx.phpscript.keep remember to use sudo before the command use in the php script.

ex:-

exec ("sudo /etc/init.d/smokeping restart 2>&1");

So in your problem,add the commands that you wish to use in to the step no (2.) as I add and change your php script as what you want.

Thusitha Sumanadasa
  • 1,521
  • 1
  • 20
  • 29
2

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.

ConroyP
  • 38,904
  • 16
  • 76
  • 86
1

It may be due to the different users running the script through the web server and the script through bash.

Normally the scripts/exec invoked through server as with user 'www' and this user dont have any write access to your area. but when you run the script in bash then you do have write permissions.

0

Default output device is changed.

login as www (after enabling) gives output through shell, but not through php.

Rudger
  • 781
  • 7
  • 8
0

I have determined that this is an issue with ImageMagick, not PHP. I am attempting a few fixes, and if they don't work, I'm going to end up using some PHP shared library (probably imagick) to do the work instead.

Alex
  • 367
  • 1
  • 4
  • 9