无法让 shell_exec 下载文件
我有一个适用于 ubuntu 的多线程 cli 下载器,名为 Aria2c。
我一直在尝试获取一个 php 脚本来运行 aria2c 并使用 shell_exec 下载文件,但我似乎无法让它工作。最终我计划在页面上有一个输入框,我可以在其中输入链接,aria 会下载它。
这是我想出的代码(现在我手动输入链接):
请注意,我指定的 aria2c 命令在 shell 中运行良好;我尝试下载的目录设置为“777”。
我很困惑为什么它不起作用,有什么想法吗?
PS:我更喜欢使用 aria 而不是其他替代方案,因为它是多线程的并且支持 cookie。
I have a multithreaded cli downloader for ubuntu called Aria2c.
I've been trying to get a php script to run aria2c and download a file using shell_exec, but i can't seem to get it to work. Ultimately I plan to have an input box on a page where I can enter a link and aria would download it.
Here's the code I've come up with (for now im inputting the link manually):<?php $dl = shell_exec('aria2c -d /home/user/ www.downloadlink.com'); ?>
Note that the aria2c command I specified works well in the shell; and the directory I'm attempting to download to is set to '777'.
I'm baffled as to why it's not working, any ideas?
PS: I prefer to use aria rather than the alternatives because it is multithreaded and it supports cookies.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
检查 PHP 是否在安全模式下运行。如果安全模式打开,
shell_exec
将不起作用。编辑:没有使用完整路径引用 aria2c。像这样引用它:
shell_exec('/path/to/aria2c -d /home/user/ www.downloadlink.com')
有效。Check if PHP is running in safe_mode.
shell_exec
won't work if safe_mode is on.EDIT: aria2c was not referenced with a full path. Referencing it like this:
shell_exec('/path/to/aria2c -d /home/user/ www.downloadlink.com')
works.我将假设您正在通过 Web 服务器运行 PHP。在这种情况下,Web 服务器不太可能有权写入用户的主目录:Apache 使用受限用户的凭据作为守护进程运行。此外,Apache 中的
PATH
环境变量不一定与用户的 PATH 相同。最后但并非最不重要的一点是,您不检查返回值或脚本输出。尝试这样的操作:
您可以通过以下方式获取 aria2c 的完整路径:
I'll make the assumption that you are running PHP through a web server. In such case, it's very unlikely that the web server has permission to write into your user's home directory: Apache runs as daemon with the credentials of a limited user. Also, the
PATH
env variable in Apache is not necessarily the same as your user's PATH. Last but not least, you don't check the return value or the script output.Try something like this:
You can get the full path for aria2c with: