单击下载链接时文件损坏

smi*_*ith 2 html php

我尝试使用php强制图像jpg文件下载,我已经实现了以下代码:

HTML

<a href = "filedownload.php?src=uploads/myimage.jpg&download=true>download this file</a>
Run Code Online (Sandbox Code Playgroud)

的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');

  } 

 ?>
Run Code Online (Sandbox Code Playgroud)

假设图像的完整路径是"www.example.com/smith/topic/uploads/myimage.jpg",我收到了正确的图像名称,并且也出现了下载窗口,但图像已损坏且1KB大小,任何人都可以告诉我为什么,非常感谢.

Far*_*ona 6

这里是一个如何使用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);
    exit;
}
?>
Run Code Online (Sandbox Code Playgroud)