duanheye7423 2015-07-03 17:07
浏览 68
已采纳

单击PHP下载图像

This code is pretty clear and neat, but for some reason it is not finding the image, so the message 'File Not Found' is being displayed after the PHP download file is reached. What is the reason for not finding the image neither by relative or by absolute path?

<?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

I've used relative path, and absolute path and it is not getting the image yet

<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>
  • 写回答

2条回答 默认 最新

  • dptrmt4366 2015-07-04 06:23
    关注

    Name the following code as 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;
    ?>
    

    and save it with your other files.

    For downloading:

    Say you put the file download.php in root folder then just give the relative path to the image/file:

    <a href="download.php?f=images/image.jpg">Download</a>
    

    or if you put download.php in say php folder inside the root folder and the images folder is also in root folder then:

    <a href="download.php?f=../images/image.jpg">Download</a>
    

    This code will work just fine for force downloading files of any extension.

    I have used pathinfo (manual http://php.net/manual/en/function.pathinfo.php) so as to get the extension and hence it reduces the code by removing the switch case you have used also it makes it more generic(i.e, the code can be used for any file type).

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行