exec() problem with long command in PHP

2019-08-05 00:43发布

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.

2条回答
兄弟一词,经得起流年.
2楼-- · 2019-08-05 00:58

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

查看更多
该账号已被封号
3楼-- · 2019-08-05 00:59

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' 
查看更多
登录 后发表回答