开发者

exec() problem with long command in PHP

开发者 https://www.devze.com 2023-03-25 04:24 出处:网络
I\'m using wkhtmltopdf on my server to transfer HTML document to PDF. It works very well when I\'m using short URL like :

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 开发者_如何学Python(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.


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


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' 
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号