exec() problem with long command in PHP


I'm using wkhtmltopdf on my server to transfer HTML document to PDF. It works very well when I'm using short URL like :

exec("/opt/wkhtmltopdf/bin/wkhtmltopdf --page-size 'Letter' --orientation 'Portrait' 'http://myurl.com/myPHPfile.php?id=12' '/tmp/myfile.pdf'")

The problem occurs when I'm using long command, like :

exec("/opt/wkhtmltopdf/bin/wkhtmltopdf --title 'The name of my file' --page-size 'Letter' --orientation 'Portrait' 'http://myurl.com/myPHPfile.php?phpsid=d8dbfbb91c0748d91426441e67aaf2b6&id=436' '/tmp/The name of my file.pdf'")

Note that when I run this long command directly from Putty it works perfectly.

The problem is that when I use exec (or shell_exec() or system() or passthru()) the page keep loading forever and my webserver doesn't respond anymore. I have to close the process from Putty (ps -x and then kill PID) myself.

Note that if I remove the ?phpsid= it works well, which is why I'm saying that the problem only occurs with long command. If I remove ?phpsid=d8dbfbb91c0748d91426441e67aaf2b6 and replace it by ?anything=ImAmAVeryLongStringThatDoNothing it doesn't work too.

I'm on CentOS 5 using WHM/cPanel. Thanks in advance for any help!


I tried urlencode(), doesn't work.<br /> I tried escapeshellarg(), the command is correctly passed but doesn't work.<br /> I tried to use short parameters, the command is correctly passed but doesn't work.

<strong>Edit 2:</strong>

Is there a string length limit while using exec(), system() or passthru()?

<strong>Edit 3:</strong>

Finally, thanks to <a href="https://stackoverflow.com/users/358679/wrikken" rel="nofollow">Wrikken</a>, the problem was that I was passing the session_id() in the URL, and then I was re-using it in the exec(). I had to add session_write_close(); before my exec() so PHP unlocks the current session to make it redable by the script in exec(). See comments below for more informations.


Let's update the comment to an answer: <strong>any and all</strong> variable arguments passed to the command line should be escaped with <a href="http://www.php.net/escapeshellarg" rel="nofollow">escapeshellarg</a>


If the command line is too long, you can use the short version of each parameter. For example, instead of:

--page-size 'Letter' --orientation 'Portrait'

you can use

-s 'Letter' --O 'Portrait'


  • Rewriting url to hide index.php and to make query nice
  • Auto Pop Up Save As Window For Firefox
  • Display Paragraphs in UITextView without \\n
  • reading large csv files in a rails app takes up a lot of memory - Strategy to reduce memory consumpt
  • Relative File Path Problem
  • Writing in file's actual position in Python
  • How to convert a subset of numpy recarray to continuous array?
  • NSFileHandle from NSURL failure
  • How to get share link with dropbox api in Swift?
  • How to use unlink in Windows 7?
  • Is java's File class' object serializable?
  • Printing list elements and strings in Python have different results
  • how to parse HTTP POST(file upload) stream?
  • How to upload excel file in angular js?
  • ssh2_scp_send() using php corrupts pdf
  • Where does the file get saved using “File file = new file(filename)” in Android
  • How to trick Node.js to load .js files as ES6 modules?
  • How to display the images in listview
  • Get the number 18437736874454810627
  • Is there a way to dynamically embed PDF Files in a JSP pulled from the file system?
  • Creating PDF from TIFF image using iText
  • Make VS2015 use angular-cli ng at build time in a .NET project
  • OpenGL 3.3 on Mac OSX El Capitan with LWJGL
  • How to create a file in java without a extension
  • MongoDB in PHP using aggregate to group by _id is null not working
  • Display issues when we change from one jquery mobile page to another in firefox
  • Jenkins: How To Build multiple projects from a TFS repository?
  • How do I fake an specific browser client when using Java's Net library?
  • To display the title for the current loaction in map in iphone
  • Rearranging Cells in UITableView Bug & Saving Changes
  • Why winpcap requires both .lib and .dll to run?
  • Apache 2.4 - remove | delete | uninstall
  • Circular dependency while pushing http interceptor
  • Run Powershell script from inside other Powershell script with dynamic redirection to file
  • Linker errors when using intrinsic function via function pointer
  • trying to dynamically update Highchart column chart but series undefined
  • Load html files in TinyMce
  • How can I get HTML syntax highlighting in my editor for CakePHP?
  • coudnt use logback because of log4j
  • FormattedException instead of throw new Exception(string.Format(…)) in .NET