磁盘上的 PHP 输出文件到浏览器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2028898/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 04:43:19  来源:igfitidea点击:

PHP output file on disk to browser

php

提问by Michel

I want to serve an existing file to the browser in PHP. I've seen examples about image/jpeg but that function seems to save a file to disk and you have to create a right sized image object first (or I just don't understand it :))

我想在 PHP 中向浏览器提供现有文件。我看过有关 image/jpeg 的示例,但该功能似乎将文件保存到磁盘,您必须先创建一个大小合适的图像对象(或者我只是不明白它:))

In asp.net I do it by reading the file in a byte array and then call context.Response.BinaryWrite(bytearray), so I'm looking for something similar in PHP.

在asp.net 中,我通过读取字节数组中的文件然后调用context.Response.BinaryWrite(bytearray) 来实现,所以我在PHP 中寻找类似的东西。

Michel

米歇尔

回答by Pekka

There is fpassthru()that should do exactly what you need. See the manual entry to read about the following example:

fpassthru()应该完全满足您的需求。请参阅手册条目以了解以下示例:

<?php

// open the file in a binary mode
$name = './img/ok.png';
$fp = fopen($name, 'rb');

// send the right headers
header("Content-Type: image/png");
header("Content-Length: " . filesize($name));

// dump the picture and stop the script
fpassthru($fp);
exit;

?>

See herefor all of PHP's filesystem functions.

有关PHP 的所有文件系统函数,请参见此处

If it's a binary file you want to offer for download, you probably also want to send the right headers so the "Save as.." dialog pops up. See the 1st answer to this questionfor a good example on what headers to send.

如果它是您想提供下载的二进制文件,您可能还想发送正确的标题,以便弹出“另存为..”对话框。有关要发送的标头的一个很好的示例,请参阅此问题的第一个答案。

回答by Knase

I use this

我用这个

  if (file_exists($file)) {


        header('Content-Description: File Transfer');
        header('Content-Type: application/octet-stream');
        header('Content-Disposition: attachment; filename='.basename($file));
        header('Content-Transfer-Encoding: binary');
        header('Expires: 0');
        header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
        header('Pragma: public');
        header('Content-Length: ' . filesize($file));

        ob_clean();
        flush();
        readfile($file);
        exit;

    } 

回答by berty

I use readfile() ( http://www.php.net/readfile)...

我使用 readfile() ( http://www.php.net/readfile) ...

But you have to make sure you set the right "Content-Type" with header() so the browser knows what to do with the file.

但是您必须确保使用 header() 设置正确的“内容类型”,以便浏览器知道如何处理该文件。

You can also force the browser to download the file instead of trying to use a plug-in to display it (like for PDFs), I always found this to look a bit "hacky", but it is explained at the above link.

您还可以强制浏览器下载文件,而不是尝试使用插件来显示它(例如 PDF),我总是觉得这看起来有点“hacky”,但在上面的链接中对此进行了解释。

回答by KiNgMaR

This should get you started: http://de.php.net/manual/en/function.readfile.php

这应该让你开始:http: //de.php.net/manual/en/function.readfile.php

Edit: If your web server supports it, using

编辑:如果您的网络服务器支持它,请使用

header('X-Sendfile: ' . $filename);

where file name contains a local path like

其中文件名包含本地路径,如

/var/www/www.example.org/downloads/example.zip

is faster than readfile().

比 readfile() 快。

(usual security considerations for using header() apply)

(使用 header() 的通常安全注意事项适用)

回答by David

For both my website and websites I create for clients I use a PHP script that I found a long time ago.

对于我的网站和我为客户创建的网站,我使用了很久以前发现的 PHP 脚本。

It can be found here: http://www.zubrag.com/scripts/download.php

可以在这里找到:http: //www.zubrag.com/scripts/download.php

I use a slightly modified version of it to allow me to obfuscate the file system structure (which it does by default) in addition to not allowing hot linking (default) and I added some additional tracking features, such as referrer, IP (default), and other such data that I might need should something come up.

除了不允许热链接(默认)之外,我使用了它的一个稍微修改过的版本,以允许我混淆文件系统结构(默认情况下),并且我添加了一些额外的跟踪功能,例如引用者、IP(默认) ,以及我可能需要的其他此类数据。

Hope this helps.

希望这可以帮助。

回答by Robert Sinclair

Following will initiate XML file output

以下将启动 XML 文件输出

$fp = fopen($file_name, 'rb');

// Set the header
header("Content-Type: text/xml");
header("Content-Length: " . filesize($file_name));
header('Content-Disposition: attachment; filename="'.$file_name.'"');

fpassthru($fp);
exit;

The 'Content-Disposition: attachment'is pretty common and is used by sites like Facebook to set the right header

“内容处置:附件”是很常见的,并使用Facebook等站点设置正确的头