如何使用php从服务器下载文件

时间:2019-05-30 11:19:03

标签: php

我有这个download.php文件,该文件旨在下载从url获取参数的文件,但我一直在获取0kb的文件。可能是什么问题?

 $file = ($_GET['file']);
 $user = ($_GET['user']);
   //The resource that we want to download.
 $filepath = $_SERVER["DOCUMENT_ROOT"]."/artistgallery/images/gallery/$user/$file";
 $len = filesize($filepath); // get size of file
  $filename = basename($filepath); // get name of file only
  $file_extension = strtolower(pathinfo($filename,PATHINFO_EXTENSION));
  //Set the Content-Type to the appropriate setting for the file
  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 "gif": $ctype="image/gif"; break;
      case "png": $ctype="image/png"; break;
      case "jpeg":
      case "jpg": $ctype="image/jpg"; break;
      default: $ctype="application/force-download";
    }
    ob_clean();
    //Begin writing headers
    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");
    //Use the switch-generated Content-Type
    header("Content-Type: $ctype");
    //Force the download
    $header="Content-Disposition: attachment; filename=".$filename.";";
    header($header );
    header("Content-Transfer-Encoding: binary");
    header("Content-Length: ".$len);
    @readfile($filepath);
    exit;

0 个答案:

没有答案