本文介绍了文件损坏时点击下载链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我尝试使用php强制图片jpg文件下载,我已经实现了以下代码:
html
< a href =filedownload.php?src = uploads / myimage.jpg& download = true>下载此档案< / a>
download.php
< ?php
ob_start();
include_once'functions.php';
if(isset($ _ GET ['download'])&& $ _GET ['download '] =='true')
{
$ src = sanitizeString($ _ GET ['src']);
header('Content-Description:File Transfer') ;
header('Content-Type:image / jpeg');
header('Content-Disposition:attachment; filename ='。basename($ src));
header('Content (''Expires:0');
header('Cache-Control:public');
header('Pragma:public');
header('Expires:0');
$ b $>
假设该图片的完整路径是www.example.com/smith/topic/uploads/myimage.jpg,我已经收到了正确的图片名称,并且下载窗口也出现了,但是图片已损坏,并且大小为1KB,任何人都可以告诉我为什么,非常感谢。
解决方案
这里是你的例子如何使用readfile函数
<?php
$ file ='monkey.gif';
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);
出口;
}
?>
I try to use php to force the image jpg file download, I have implemented eth following code:
html
<a href = "filedownload.php?src=uploads/myimage.jpg&download=true>download this file</a>
download.php
<?php
ob_start();
include_once 'functions.php';
if (isset($_GET['download']) && $_GET['download'] == 'true')
{
$src = sanitizeString($_GET['src']);
header('Content-Description: File Transfer');
header('Content-Type: image/jpeg');
header('Content-Disposition: attachment; filename='.basename($src));
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: public');
header('Pragma: public');
}
?>
Suppose the full path of the image is "www.example.com/smith/topic/uploads/myimage.jpg", I have recieved the right image name and the download window is appeared as well, but the image is corrupt and with 1KB size, any one could tell me why, thanks a lot.
解决方案
Here you are example how to use readfile function
<?php
$file = 'monkey.gif';
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;
}
?>
这篇关于文件损坏时点击下载链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!