使用 PHP readfile 出现零星损坏的图像
我搜索了类似的请求/问题,但似乎没有任何内容符合我的情况。
我使用 PHP 文件将上传的文件发送/输出到浏览器。我将 PHP 文件称为“阅读器”文件。以下是阅读器的精简版本(为了简单起见,删除了其他文件类型):
<?php
if($_GET['upload']){ // for files uploaded via CMS uploader
$file = $site_directory . $uploads_path . $_GET['upload'];
}
if (file_exists($file)) {
header('Content-Description: File Transfer');
// JPG
if(strstr($file,".jpg"))
{
header('Content-type: image/jpeg');
header('Content-Disposition: inline; filename='.basename($file));
// OTHER FILE TYPES
}else
{
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;
}else{
echo "<p>That file does not exist.</p>";
}
?>
正如您将在下面的示例中看到的,它通常可以工作。但是,当浏览器必须以这种方式加载多个图像时,有些图像将无法加载/显示为损坏。您可以在此处查看其实际操作示例:http://www. technotarek.com/index.php?id=25&view=grid。如果您一开始在照片网格中没有看到任何损坏的图像,请尝试几次硬刷新。
有什么想法为什么这对于多个文件/图像不可靠?
请注意,我有一个额外的脚本可以重新调整示例中图像的大小,但这不是罪魁祸首。我把它删除了,问题仍然存在。
更多背景(关于我为什么使用这种方法):我最初开始在服务器管理员不允许上传到网络服务器的网站上使用阅读器脚本/文件,而是要求所有用户上传存储在单独的沙地中服务器。 “reader”文件允许我访问和输出这些文件,因为无法直接使用沙地文件的 URL。
I searched similar requests/questions, but nothing seems to match my situation.
I use a PHP file to send/output uploaded files to the browser. I call the PHP file a "reader" file. The following is a boiled down version of the reader (with additional file types removed for simplicity):
<?php
if($_GET['upload']){ // for files uploaded via CMS uploader
$file = $site_directory . $uploads_path . $_GET['upload'];
}
if (file_exists($file)) {
header('Content-Description: File Transfer');
// JPG
if(strstr($file,".jpg"))
{
header('Content-type: image/jpeg');
header('Content-Disposition: inline; filename='.basename($file));
// OTHER FILE TYPES
}else
{
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;
}else{
echo "<p>That file does not exist.</p>";
}
?>
As you'll see in the following example, it generally works. But when the browser has to load several images this way, some won't load/appear as broken. You can see an example of it in action here: http://www.technotarek.com/index.php?id=25&view=grid. If you don't see any broken images in the photo grid at first, try a few hard refreshes.
Any ideas why this isn't reliable for multiple files/images?
Note that I have an additional script that re-sizes the images on the example, but that is not the culprit. I removed it and the problem remained.
More Background (on why I'm using this method): I originally started using the reader script/file on sites where the server admins didn't allow uploads to the webserver, but instead required all user uploads to be stored on a separate sandlot server. The "reader" file allows me to access and output those files, because a direct URL to the sandlot files is not possible.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您的间接下载脚本只是用于添加标头,那么请调查是否可以使用:
mod_cern_meta
mod_headers
为了减少脚本负载,可以替代
readfile
会为:mod_xsendfile
和X-Sendfile:
标头,因此 Apache 进程处理读取/发送。 (如果文件通过网络文件系统以某种方式映射,应该是可能的。)If your indirect-download script just serves to add header, then investiage if you can use:
mod_cern_meta
mod_headers
To reduce script load, an alternative to
readfile
would be:mod_xsendfile
and theX-Sendfile:
header, so the Apache process handles the reading/sending. (Should be possible if the files are mapped somehow via a network filesystem.)