强制下载提示php损坏文件

Posted

技术标签:

【中文标题】强制下载提示php损坏文件【英文标题】:Force download prompt php broken file 【发布时间】:2011-12-08 10:16:52 【问题描述】:

我现在有当前代码:

if($_POST['mode']=="save") 
    $path = $_POST['path'];
    $file = end(explode('/', $path));
    header('Content-Description: File Transfer');
    header('Content-Type: application/octet-stream');
    header("Content-Disposition: attachment; filename=$file");
    readfile($file);

我所做的是,获取保存文件的路径,将其分解为仅文件名并尝试保存该文件。我确实收到了另存为提示,但是当我保存它并尝试打开文件时,我收到文件/图像损坏的错误消息。

文件不保存在主目录中,而是保存在名为 uploads 的子目录中。

有人知道我做错了什么吗?

提前致谢。 思南

【问题讨论】:

保存文件的代码是什么?您确定文件已正确保存吗?在读取文件之前,您是否尝试将 readfile() 包含在 is_readable() ... 中? @macjohn 我刚刚注意到我的文件不可读。为什么不呢? is_readable() 如果文件名指定的文件或目录存在并且可读,则返回 TRUE,否则返回 FALSE。您的文件不存在,或者您没有权限读取它。 文件/目录现在可读了,但还是无法显示图片。 @macjohn 正在保存的文件大小正确,文件名正确但由于某种原因不想打开。 【参考方案1】:

我通常使用这样的东西:

$siteEmail = 'me@myemail.com';
$companyName = 'My Company';

#   Path from server root
$fileDirectory = '/home/uploads/'; // include trailing slash

// get the file reference
$passedFile = $_POST['path'];
$filename = @urldecode($passedFile); // useful if your file is sent by $_GET
if((!isset($passedFile))||(@$passedFile=='')) 
    echo "<html><title>".$companyName." - Download File</title><body>ERROR: Please pass a valid filename.<br /><br />Please go back and try again. If you continue to see this message please contact <a href=\"mailto:".$siteEmail."\">".$siteEmail."</a></body></html>";exit();


// required for IE, otherwise Content-disposition is ignored
if(ini_get('zlib.output_compression'))
  ini_set('zlib.output_compression', 'Off');

$file_extension = strtolower(substr(strrchr($filename,"."),1));

if( $filename == "" ) 
  echo "<html><title>".$companyName." - Download File</title><body>ERROR: download file NOT SPECIFIED.<br /><br />Please go back and try again. If you continue to see this message please contact <a href=\"mailto:".$siteEmail."\">".$siteEmail."</a></body></html>";
  exit;
 elseif ( ! file_exists( $fileDirectory . $filename ) ) 
  echo "<html><title>".$companyName." - Download File</title><body>ERROR: File not found.<br /><br />Please go back and try again. If you continue to see this message please contact <a href=\"mailto:".$siteEmail."\">".$siteEmail."</a>
  <p>path: ".$fileDirectory.$filename."</p>
  <p>DOCUMENT_ROOT: ".$_SERVER['DOCUMENT_ROOT']."</p>
  </body></html>";
  exit;
;

switch( $file_extension ) 
  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 "rtf": $ctype="application/rtf"; break;
  case "gif": $ctype="image/gif"; break;
  case "png": $ctype="image/png"; break;
  case "wav": $ctype="audio/wav"; break;
  case "mp3": $ctype="audio/mpeg3"; break;
  case "wmv": $ctype="video/x-ms-wmv"; break;
  case "avi": $ctype="video/avi"; break;
  case "asf": $ctype="video/x-ms-asf"; break;
  case "mpg": $ctype="video/mpeg"; break;
  case "mpeg": $ctype="video/mpeg"; break;
  case "jpeg":
  case "jpg": $ctype="image/jpg"; break;
  default: $ctype="application/force-download";


// Everything went fine - you could log the download in the databse here if required?

// OUTPUT THE FILE

header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); // cache stop
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); // cache stop
header("Cache-Control: must-revalidate"); // cache stop
header("Content-Type: $ctype"); // output filetype
header("Content-Disposition: attachment; filename=\"".basename($filename)."\";" );
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".filesize($fileDirectory.$filename));
readfile($fileDirectory.$filename);
exit(); 

【讨论】:

以上是关于强制下载提示php损坏文件的主要内容,如果未能解决你的问题,请参考以下文章

PHP强制下载.xlsx文件损坏

强制下载文件损坏的标题错误?

U盘删除文件时提示“文件或目录损坏且无法读取”的解决方法

ubuntu强制关机导致文件系统损坏

Javascript下载文件强制提示目的地

使用“另存为”提示强制下载文件