无法使用 PHP 查看下载文件中的数据
unable to view data from downloaded file using PHP
我已经编写了下载文件的代码。使用此代码我正在下载文件,但是当我打开文件时我看不到任何数据。下面是我正在使用的代码。
<?php
$namekey = $_REQUEST['key'];
$tmp = explode(".",$file['namekey']);
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";
}
$location="uploads/sop/".$namekey."";
$fszie=filesize($location);
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=\"".$namekey."\";" );
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".$fsize);
ob_clean();
flush();
//readfile( 'uploads/sop/'.$file['namekey'] );
readfile( $location);
?>
code.Any 帮助中是否有任何错误,将不胜感激!
提前致谢!
查看前两行代码:
<?php
$namekey = $_REQUEST['key'];
$tmp = explode(".",$file['namekey']);
这里 $file['namekey'] 是未定义的,你应该使用 $namekey:
<?php
$namekey = $_REQUEST['key'];
$tmp = explode(".",$namekey);
不确定到底是什么原因....但是,删除下面的代码后它工作正常......
header("Content-Length: ".$fsize);
谢谢
我已经编写了下载文件的代码。使用此代码我正在下载文件,但是当我打开文件时我看不到任何数据。下面是我正在使用的代码。
<?php
$namekey = $_REQUEST['key'];
$tmp = explode(".",$file['namekey']);
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";
}
$location="uploads/sop/".$namekey."";
$fszie=filesize($location);
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=\"".$namekey."\";" );
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".$fsize);
ob_clean();
flush();
//readfile( 'uploads/sop/'.$file['namekey'] );
readfile( $location);
?>
code.Any 帮助中是否有任何错误,将不胜感激!
提前致谢!
查看前两行代码:
<?php
$namekey = $_REQUEST['key'];
$tmp = explode(".",$file['namekey']);
这里 $file['namekey'] 是未定义的,你应该使用 $namekey:
<?php
$namekey = $_REQUEST['key'];
$tmp = explode(".",$namekey);
不确定到底是什么原因....但是,删除下面的代码后它工作正常......
header("Content-Length: ".$fsize);
谢谢