3

I have the following script to take an uploaded PDF in PHP and call ImageMagik/Ghostscript to convert to images of specified size.

$tmp = exec("convert \"{$strPDF}\" -resize 500X500 \"{$strOut}\"", $results); 

However, this doesnt seem to work. No errors in the log file and no errors on screen. If I do the following,

$tmp = exec("convert \"{$strPDF}\" -resize 500X500 \"{$strOut}\"", $results); 
echo ("convert \"{$strPDF}\" -resize 500X500 \"{$strOut}\"");

And I paste the output into a command prompt it works fine (It takes around 6-10 seconds - My max_execution_time is at 600.

Any suggestions on why this might not be working?

This is Windows, IIS 7 and PHP5.

Edit: I'm having the same issue in both CentOS and Windows. Both have ImageMagik and Ghostscript installed.

Edit Edit the below still fails.

$handle = popen("convert \"{$strPDF}\" -resize 500X500 \"{$strOut}\"","r");
        echo "'$handle'; " . gettype($handle) . "\n";
        $read = fread($handle, 2096);
        echo $read;
        pclose($handle);
5
  • 1
    You are asking for trouble by putting variables directly in the command line. CLI Injection is as dangerous, if not more dangerous than SQL Injection. Use escapeshellarg(). Commented Sep 6, 2010 at 14:47
  • @Andrew Comment noted, thanks. Commented Sep 6, 2010 at 15:22
  • Try using popen instead of exec and read the output (with fgets) Commented Sep 6, 2010 at 15:24
  • @Milan Babuškov Same issue. :( Updated main post with code. Commented Sep 6, 2010 at 15:33
  • Does $strPDF or $strOut contain spaces? Commented Sep 13, 2010 at 11:29

3 Answers 3

10

Try adding 2>&1 to the end of your command. I found it here.

exec('some_command 2>&1', $output);
print_r($output);  // to see the respond to your command
Sign up to request clarification or add additional context in comments.

Comments

3

in php.ini maybe this function exec declare under

disable_functions

or if you run in

safe mode

This function is disabled when PHP is running in safe mode.

2 Comments

(safe_mode = Off and disable_functions = ) So doesnt look like it is that.
maybe permission on this file or this directory
1

Could it be an issue with your PATH variable ?

You might want to try and run

<?php
echo exec('cmd.exe /c echo %path%');
?>

both in the command line and from IIS and compare their outputs.

1 Comment

Might help if you try using absolute paths in this case (especially if even "cmd.exe" does not execute).

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.