PHP 将文件句柄传递给用户,以便文件下载和下载 保存到他们的机器上

发布于 2024-07-13 16:58:35 字数 849 浏览 3 评论 0原文

我正在从另一台服务器下载文件。 我希望将此文件推送给我的用户,而不是将其保存到我的服务器上。

换句话说,将文件句柄传递给他们,这样文件就会通过我的服务器并保存到他们的计算机上。 我怎样才能做到这一点? 到目前为止我已经做到了:

$handle = fopen($_GET['fileURL'], 'r');
$filename = stream_get_contents($handle);

如何将其推送给用户,也许使用标头?

感谢您的任何帮助和指导。

编辑

我有标题:

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 秒后我只得到一个空白页,看起来它下载了文件但没有将其提供给我。

我希望脚本立即将标头作为流发送给用户。 出口();

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?

Thank you for any help and direction.

EDIT

I have the 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");

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.

I wish for the script to immediately send the headers to the user as a stream.
exit();

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

迷你仙 2024-07-20 16:58:35

您可以尝试此

$filetype = mime_content_type($filename);
header('Content-type: '.$filetype);
header('Content-Disposition: attachment; filename="'.$filename.'"');

更新进行编辑:

您是否禁用了错误,因为这听起来像是标头已发送错误?

error_reporting(E_ALL | E_STRICT);

You can try this

$filetype = mime_content_type($filename);
header('Content-type: '.$filetype);
header('Content-Disposition: attachment; filename="'.$filename.'"');

UPDATE for your EDIT:

Do you have errors disabled, since this sounds like the headers already sent error?

error_reporting(E_ALL | E_STRICT);
泪冰清 2024-07-20 16:58:35

使用readfile()时不必使用fopen();

只需将文件名包含在 readfile() 中,如下所示:

readfile($_GET['fileUrl']);

尽管这在安全方面非常危险,因为用户可以指定文件服务器上的任何文件。 如果您只有几个文件,您希望其他人能够下载,也许您应该将它们存储在数组(或数据库,最好)中,

这是一个数组示例:

$files = array('file1.jpg', 'file2.png', 'file3.pdf');
//assume $_GET['file_id'] == 0, 1 or 2

if (file_exists($files[$_GET['file_id']]))
    readfile($files[$_GET['file_id']]);

You don't have to use fopen() when using readfile();

Just include the filename inside readfile() like this:

readfile($_GET['fileUrl']);

Although this is very dangerous security-wise as the user could specify any file on your file server. If you only have a few files you want someone to be able to download perhaps you should store them in an array (or database, preferebly)

Here's an array example:

$files = array('file1.jpg', 'file2.png', 'file3.pdf');
//assume $_GET['file_id'] == 0, 1 or 2

if (file_exists($files[$_GET['file_id']]))
    readfile($files[$_GET['file_id']]);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文