exec() problem with long command in PHP

∥☆過路亽.° 提交于 2019-12-13 19:39:03

问题


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!

Edit:

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

Edit 2:

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

Edit 3:

Finally, thanks to Wrikken, 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.


回答1:


Let's update the comment to an answer: any and all variable arguments passed to the command line should be escaped with escapeshellarg




回答2:


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' 


来源:https://stackoverflow.com/questions/6928171/exec-problem-with-long-command-in-php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!