我现在有了当前的代码:
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);
}
我所做的是,获取保存文件的路径,将其分解为仅文件名并尝试保存该文件。我确实得到了保存为提示,但是当我保存它并尝试打开文件时,我收到文件/图像损坏的错误消息。
这些文件不会保存在homedirectory中,而是保存在名为uploads的子目录中。
有人知道我做错了什么吗?
提前致谢。 思南
答案 0 :(得分:0)
我通常使用这样的东西:
$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();