tags:

views:

8032

answers:

10

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

+2  A: 

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

Tomh
A: 

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
A: 

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
A: 

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
A: 

What are the arguments being passed to convert? Do they include the full path to the file?

R. Bemrose
+3  A: 

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
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.
A: 

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.

A: 

I've been trying to do the same thing with the same problem. Now I'm using GhostScript, but i was wondering what syntax you used for the Ghostscript command. I seem to be getting and error now with GhostScript <-- GPL Ghostscript 8.63 (2008-08-01) Copyright (C) 2008 Artifex Software, Inc. All rights reserved. This software comes with NO WARRANTY: see the file PUBLIC for details. Processing pages 1 through 1. Page 1 Error: /invalidfileaccess in --showpage-- Operand stack: --nostringval-- 1 true Execution stack: %interp_exit .runexec2 --nostringval-- --nostringval-- --nostringval-- 2 %stopped_push --nostringval-- --nostringval-- --nostringval-- false 1 %stopped_push 1905 1 3 %oparray_pop 1904 1 3 %oparray_pop 1888 1 3 %oparray_pop --nostringval-- --nostringval-- 2 1 1 --nostringval-- %for_pos_int_continue --nostringval-- --nostringval-- 1777 1 9 %oparray_pop --nostringval-- --nostringval-- Dictionary stack: --dict:1151/1684(ro)(G)-- --dict:1/20(G)-- --dict:75/200(L)-- --dict:75/200(L)-- --dict:106/127(ro)(G)-- --dict:275/300(ro)(G)-- --dict:22/25(L)-- --dict:4/6(L)-- --dict:21/40(L)-- Current allocation mode is local Last OS error: 13 <-- There is a copy of the error returned in my browser from the gs command.

Thnx :)

A: 

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.

A: 

Default output device is changed.

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

Rudger