PHP Pass文件句柄给用户,以便文件下载&保存到他们的机器上 [英] PHP Pass File Handle to user so that file downloads & saves to their machine
问题描述
我正从其他服务器下载文件。我希望将此文件推送给我的用户,而不是将其保存到我的服务器。
I am downloading a file from another server. I wish to push this file to my users rather than saving it to my server.
换句话说,将它们传递给文件句柄,这样它就可以通过我的服务器并保存到他们的机器上。我怎样才能做到这一点?到目前为止我有这个:
In other words, pass them the file handle so it just passes through my server and saves to their machine. How can I do this? I have this so far:
$handle = fopen($_GET['fileURL'], 'r');
$filename = stream_get_contents($handle);
如何将此推送给用户,可能使用标题?
How do I push this to the user, maybe using headers?
感谢您的帮助和指导。
我有标题:
header("Pragma: public"); // required
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: private",false);
header("Content-Type: $ctype");
header("Content-Disposition: attachment; filename=\"".basename($filename)."\";" );
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".filesize($filename));
readfile("$filename");
它只是不推动标题。我只是在大约15秒之后得到一个空白页面,看起来它下载文件但没有给我。
Its just that it doesn't push the headers. I just get a blank page after about 15 seconds which looks like it downloading the file but not giving it to me.
我希望脚本立即发送标题到用户作为流。
exit();
I wish for the script to immediately send the headers to the user as a stream. exit();
推荐答案
你可以试试这个
$filetype = mime_content_type($filename);
header('Content-type: '.$filetype);
header('Content-Disposition: attachment; filename="'.$filename.'"');
您的编辑更新:
你有错误被禁用,因为这听起来像已经发送的标题错误?
Do you have errors disabled, since this sounds like the headers already sent error?
error_reporting(E_ALL | E_STRICT);
这篇关于PHP Pass文件句柄给用户,以便文件下载&保存到他们的机器上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!