简体   繁体   English

使用 PHP 点击下载图片

[英]Download image by clicking with 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.这段代码非常清晰整洁,但由于某种原因它没有找到图像,因此在到达 PHP 下载文件后会显示消息“找不到文件”。 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 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>

Name the following code as download.php将以下代码命名为 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:假设您将文件 download.php 放在根文件夹中,然后只提供图像/文件的相对路径:

<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:或者,如果您将 download.php 放在根文件夹内的 php 文件夹中,而图像文件夹也在根文件夹中,则:

<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 (ie, the code can be used for any file type).我使用了路径信息(手册http://php.net/manual/en/function.pathinfo.php )以获得扩展,因此它通过删除您使用的开关盒来减少代码,它也使它更通用(即,代码可用于任何文件类型)。

This code works great, just delete此代码效果很好,只需删除

path/<?=$row['/images/image01.jpg']?>

and just add the name file: image01.jpg like只需添加名称文件:image01.jpg 就像

<a href="/php/download.php?file=image01.jpg">Download1</a>

Then move your php file to the directory where your images are uploaded.然后将您的 php 文件移动到您上传图片的目录。

Hope this help to somebody.希望这对某人有所帮助。 Greetings.你好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM