使用 PHP 和 cURL 提供下载服务
我正在编写一个脚本,使用我的 Megaupload 高级帐户为用户提供下载服务。这是我用来下载 Megaupload 文件的方法:
public function download_file($link)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $link);
curl_setopt($ch, CURLOPT_COOKIEFILE, dirname(__FILE__) . "/cookies/megaupload.txt");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
return curl_exec($ch);
curl_close($ch);
}
这就是它在 index.php 中的调用方式:
readfile($megaupload->download_file("http://www.megaupload.com/?d=PXNDZQHM"));
我基本上想要做的是向用户提供下载并通过我的服务器流式传输下载。我不希望将文件保存到我的服务器然后提供给用户。
但显然,上面的解决方案是行不通的。我收到此错误:
Warning: readfile() [function.readfile]: Filename cannot be empty in C:\xampp\htdocs\index.php on line 8
我需要为此使用 cURL,因为我需要使用 cookie 文件 (/cookies/megaupload.txt) 请求下载。
有人能想出一种可行的方法来做到这一点吗?干杯。
I am writing a script that serves users downloads using my Megaupload premium account. This is the method that I'm using to download the Megaupload files:
public function download_file($link)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $link);
curl_setopt($ch, CURLOPT_COOKIEFILE, dirname(__FILE__) . "/cookies/megaupload.txt");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
return curl_exec($ch);
curl_close($ch);
}
And this is how it's being called in index.php:
readfile($megaupload->download_file("http://www.megaupload.com/?d=PXNDZQHM"));
What I basically want this to do is serve the download to the user and stream the download through my server. I do not want the file to be saved to my server and then offered to the user.
But obviously, the solution above won't work. I get this error:
Warning: readfile() [function.readfile]: Filename cannot be empty in C:\xampp\htdocs\index.php on line 8
I need to use cURL for this because I need to request the download using a cookie file (/cookies/megaupload.txt).
Can anyone think of a working way to do this? Cheers.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用您尝试使用的方法将不起作用(或者不会像您想象的那样起作用)。
Curl 将阻塞,直到文件完全下载。
您必须创建自己的协议,并处理类中的写入事件。
这是之前讨论的链接,其中有人做了很好的解释。基本上,您需要以可管理的块来处理文件并将它们交给客户端的浏览器: 操作一个长度为 3000 万个字符的字符串
您也可以尝试使用套接字,但您必须处理你的cookie代码,因为curl会在幕后为你处理很多事情。
Using the method you're trying to use won't work (or won't work like you think).
Curl will block until the file is completely downloaded.
You have to create your own protocol, and handle the write event within the class.
Here's a link to a previous discussion where someone has done a very good job of explaining it. Basically you need to process the file in manageable chunks and hand them off to the client's browser: Manipulate a string that is 30 million characters long
You could also try using sockets, but you're going to have to work on your cookie code, because curl handles a lot of that behind the scenes for you.
是的,正如它所说,readfile 需要一个文件名,并且您给它一个curl 结果的字符串。只需在 设置正确的标头后使用 echo:
编辑:您还需要进行一些基本检查,确保您收到了回复:
然后:
Yes, exactly as it says,
readfile
expects a filename, and you're giving it a string of the curl result. Just use echo after setting the proper headers:EDIT: Also you need to do some basic checks that you got something back:
Then:
算法
Algorithm for that