当我尝试使用php下载文件时,文件为空

时间:2014-06-28 03:53:23

标签: php

好吧,我有一个名为upload/doc的文件夹,其中保存了所有上传的文件。之后我可以选择下载上传的文件。所以我正在使用以下代码:

<a target="_blank" href="upload_doc/<?php echo $rselectfile1['file_name']; ?>">Download</a>

它已成功下载该文件,但我不希望这样,以便我使用另一个以下代码:

<a target="_blank" href="download.php?filename=<?php echo $rselectfile1['file_name']; ?>">Download</a>

Download.php代码:

<?php
@session_start();
//require_once("config.php");
if(!isset($_SESSION['front_username']) && isset($_SESSION['front_username']) == "" &&
    !isset($_SESSION['front_password']) && isset($_SESSION['front_password']) == "" &&
     !isset($_SESSION['user_id']) && isset($_SESSION['user_id']) == "") {
    header("Location:login.php");   
    exit();
}


$file_get = $_GET['filename'];

$tmp = explode(".",$file_get);
switch ($tmp[count($tmp)-1]) {
  case "pdf": $ctype="application/pdf"; break;
  case "exe": $ctype="application/octet-stream"; break;
  case "zip": $ctype="application/zip"; break;
  case "docx":
  case "doc": $ctype="application/msword"; break;
  case "csv":
  case "xls":
  case "xlsx": $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;
  case "tif":
  case "tiff": $ctype="image/tiff"; break;
  case "psd": $ctype="image/psd"; break;
  case "bmp": $ctype="image/bmp"; break;
  case "ico": $ctype="image/vnd.microsoft.icon"; 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=\"".$file_get."\";" );
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".$fsize);
ob_clean();
flush();
readfile("upload_doc/$file_get" );
?>

但是这样下载的文件是 !!你知道为什么它是空的,你可以帮我解决它吗?

谢谢你:)

1 个答案:

答案 0 :(得分:0)

变化:

header("Content-Length: ".$fsize);

为:

header("Content-Length: ".filesize("upload_doc/$file_get"));

因为您从未设置$fsize。空Content-Length标题被视为零长度,因此您将获得一个空文件。