PHP 中的 exec() 长命令问题
我在我的服务器上使用 wkhtmltopdf 将 HTML 文档传输为 PDF。当我使用短 URL 时,它工作得很好,例如:
exec("/opt/wkhtmltopdf/bin/wkhtmltopdf --page-size 'Letter' --orientation 'Portrait' 'http://myurl.com/myPHPfile.php?id=12' '/tmp/myfile.pdf'")
当我使用长命令时,就会出现问题,例如:
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'")
请注意,当我直接从 Putty 运行这个长命令时,它工作得很好。
问题是,当我使用 exec (或 shell_exec() 或 system() 或 passthru())时,页面会永远加载,并且我的网络服务器不再响应。我必须自己从 Putty 中关闭进程(ps -x 然后杀死 PID)。
请注意,如果我删除 ?phpsid= 它效果很好,这就是为什么我说问题只发生在长命令中。如果我删除 ?phpsid=d8dbfbb91c0748d91426441e67aaf2b6 并将其替换为 ?anything=ImAmAVeryLongStringThatDoNothing 它也不起作用。
我在 CentOS 5 上使用 WHM/cPanel。 预先感谢您的任何帮助!
编辑:
我尝试了 urlencode(),但不起作用。
我尝试了 escapeshellarg(),命令正确传递但不起作用。
我尝试使用短参数,命令正确传递但不起作用。
编辑2:
使用 exec()、system() 或 passthru() 时是否有字符串长度限制?
编辑3:
最后,感谢Wrikken,问题是我传递了session_id( ) 在 URL 中,然后我在 exec() 中重新使用它。我必须添加 session_write_close();在我的 exec() 之前,PHP 会解锁当前会话,使其可由 exec() 中的脚本重新编辑。请参阅下面的评论以获取更多信息。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
让我们将注释更新为答案:传递到命令行的任何和所有变量参数都应该使用
escapeshellarg
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:
you can use