php强制下载脚本不再工作


php force download script no longer works

我们一直在使用以下脚本下载照片,但现在它已经不起作用了。在is_file函数中,如果它不是一个文件,它会像应该的那样重定向到图像所在的实际图像位置。如果我把它拿出来,它会下载,但jpg有一个错误。不确定,因为图像在那里(如果它不下载,你会被重定向到它)。

<?php
  ini_set('memory_limit', '-1');
  $sourceFile = $_GET['file'];
  $sourceFile = urldecode($sourceFile);

  if( headers_sent() )
    die('Headers Sent');
  if(ini_get('zlib.output_compression'))
    ini_set('zlib.output_compression', 'Off');
  if (!is_file( $sourceFile ))
    header( 'Location:'. $sourceFile ) ;
  $len = filesize($sourceFile);
  $filename = basename($sourceFile);
  $file_extension = strtolower(substr(strrchr($filename,"."),1));
  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 ".docx": $ctype="application/vnd.openxmlformats-officedocument.wordprocessingml.document"; break;
    case ".pptx": $ctype="application/vnd.openxmlformats-officedocument.presentationml.presentation"; break;
    case ".xlsx": $ctype="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"; break;
    case "gif"  : $ctype="image/gif"; break;
    case "png"  : $ctype="image/png"; break;
    case "jpeg" :
    case "jpg"  : $ctype="image/jpg"; break;
    case "mp3"  : $ctype="audio/mpeg"; break;
    case "wav"  : $ctype="audio/x-wav"; break;
    case "mpeg" :
    case "mpg"  :
    case "mpe"  : $ctype="video/mpeg"; break;
    case "mov"  : $ctype="video/quicktime"; break;
    case "avi"  : $ctype="video/x-msvideo"; break;
    case "mp4"  : $ctype="video/mpeg"; break;
    //The following are for extensions that shouldn't be downloaded
    case "php"  :
    case "css"  :
    case "js"   :
    case "htm"  :
    case "html" :
    case "txt"  : die("<b>Cannot be used for ". $file_extension ." files!</b>"); break;
    default     : $ctype="application/force-download";
  }
  header("Pragma: public");
  header("Expires: 0");
  header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
  header("Cache-Control: public");
  header("Content-Description: File Transfer");
  header("Content-Type: $ctype");
  $header="Content-Disposition: attachment; filename=".$filename.";";
  header($header );
  header("Content-Transfer-Encoding: binary");
  header("Content-Length: ".$len);
  echo file_get_contents($sourceFile);
  exit;

有很多事情可能会导致is_file()失败:

  1. 你的环境在工作和停止工作之间发生了什么变化?请确保检查文件权限,包括父目录
  2. 它适用于某些图像而不适用于其他图像吗?可能是您的文件大小大于整数存储。根据手册:

    注意:由于PHP的整数类型是有符号的,并且许多平台使用32位整数,因此一些文件系统函数可能会为大于2GB的文件返回意外结果。

  3. 文件名中是否包含任何外来字符?尝试移除它们
  4. 您是否尝试使用完全绝对路径/var/www/html/images/file.jpg而不是相对路径/images/file.jpg
  5. 您是否尝试过file_exists()之类的替代函数

关于第二个问题,即使没有文件检查,.jpg也会产生错误。

  1. 到底是什么错误,还是你的意思是下载损坏的文件

我复制&将您的代码粘贴到我的本地测试环境中,它看起来工作正常,没有任何错误。如果以上信息没有帮助,请用更多详细信息更新您的问题。