这段代码非常清晰和整洁,但出于某种原因,它没有找到图像,所以在到达PHP下载文件后,将显示消息‘Found’。不通过相对路径或绝对路径找到图像的原因是什么?
<?php
$file = $_GET['file'];
download_file($file);
function download_file( $fullPath ){
// Must be fresh start
if( headers_sent() )
die('Headers Sent');
// Required for some browsers
if(ini_get('zlib.output_compression'))
ini_set('zlib.output_compression', 'Off');
// File Exists?
if( file_exists($fullPath) ){
// Parse Info / Get Extension
$fsize = filesize($fullPath);
$path_parts = pathinfo($fullPath);
$ext = strtolower($path_parts["extension"]);
// Determine Content Type
switch ($ext) {
case "pdf": $ctype="application/pdf"; break;
case "exe": $ctype="application/octet-stream"; break;
case "zip": $ctype="application/zip"; break;
case "doc": $ctype="application/msword"; break;
case "xls": $ctype="application/vnd.ms-excel"; break;
case "ppt": $ctype="application/vnd.ms-powerpoint"; break;
case "gif": $ctype="image/gif"; break;
case "png": $ctype="image/png"; break;
case "jpeg":
case "jpg": $ctype="image/jpg"; break;
default: $ctype="application/force-download";
}
header("Pragma: public"); // required
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: private",false); // required for certain browsers
header("Content-Type: $ctype");
header("Content-Disposition: attachment; filename=\"".basename($fullPath)."\";" );
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".$fsize);
ob_clean();
flush();
readfile( $fullPath );
} else
die('File Not Found');
}
?>HTML
我使用了相对路径和绝对路径,它还没有得到图像
<a href="/php/download.php?file=path/<?=$row['/images/image01.jpg']?>">Download</a>
<a href="/php/download.php?file=path/<?=$row['http://***.com/images/image01.jpg']?>">Download</a>发布于 2015-07-04 06:23:17
将下列代码命名为download.php
<?php
$file = $_GET['f'];
header("Expires: 0");
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");
$ext = pathinfo($file, PATHINFO_EXTENSION);
$basename = pathinfo($file, PATHINFO_BASENAME);
header("Content-type: application/".$ext);
// tell file size
header('Content-length: '.filesize($file));
// set file name
header("Content-Disposition: attachment; filename=\"$basename\"");
readfile($file);
// Exit script. So that no useless data is output.
exit;
?>把它和你的其他文件一起保存。
下载:
假设您将文件download.php放在根文件夹中,然后给出图像/文件的相对路径:
<a href="download.php?f=images/image.jpg">Download</a>或者,如果您将download.php放在根文件夹内的php文件夹中,并且图像文件夹也位于根文件夹中,那么:
<a href="download.php?f=../images/image.jpg">Download</a>这段代码对于强制下载任何扩展名的文件都很好。
为了获得扩展名,我使用了pathinfo (手动http://php.net/manual/en/function.pathinfo.php),因此它通过删除已使用的开关情况来减少代码,还使用它使其更通用的(即,代码可以用于任何文件类型)。
发布于 2015-07-03 21:09:03
这段代码很好用,只需删除。
path/<?=$row['/images/image01.jpg']?>只需添加名称文件: image01.jpg,类似
<a href="/php/download.php?file=image01.jpg">Download1</a>然后将php文件移动到上传图像的目录中。
希望这对某人有帮助。欢迎光临。
https://stackoverflow.com/questions/31211560
复制相似问题