强制下载提示php断开文件
我现在有了当前代码:强制下载提示php断开文件,php,file,header,save,prompt,Php,File,Header,Save,Prompt,我现在有了当前代码: 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: attachm
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的子目录中
有人知道我做错了什么吗
提前谢谢。
锡南我通常使用这样的东西:
$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();
$siteEmail=”me@myemail.com';
$companyName=‘我的公司’;
#来自服务器根目录的路径
$fileDirectory='/home/uploads/';//包括尾随斜杠
//获取文件引用
$passedFile=$\u POST['path'];
$filename=@urldecode($passedFile);//如果您的文件是由$\u GET发送的,则非常有用
如果((!isset($passedFile))| |(@$passedFile==“”)){
echo“$companyName”-下载文件错误:请传递有效的文件名。
请返回并重试。如果继续看到此消息,请联系“exit()”;
}
//IE必需,否则将忽略内容配置
if(ini_get('zlib.output_compression'))
ini_集('zlib.output_compression','Off');
$file_extension=strtolower(substr(strrchr($filename,“.”),1));
如果($filename==“”){
echo“$companyName”-下载文件错误:未指定下载文件。
请返回并重试。如果您继续看到此消息,请联系”;
出口
}elseif(!file_存在($fileDirectory.$filename)){
echo“$companyName”-下载文件错误:找不到文件。
请返回并重试。如果继续看到此消息,请与联系
路径:“.$fileDirectory.$filename。”
DOCUMENT\u ROOT:“.$”服务器['DOCUMENT\u ROOT']
";
出口
};
开关($file\u扩展名){
案例“pdf”:$ctype=“application/pdf”;中断;
案例“exe”:$ctype=“应用程序/八位字节流”;中断;
案例“zip”:$ctype=“application/zip”;中断;
案例“doc”:$ctype=“application/msword”;中断;
案例“xls”:$ctype=“application/vnd.ms excel”;中断;
案例“ppt”:$ctype=“application/vnd.ms powerpoint”;中断;
案例“rtf”:$ctype=“应用程序/rtf”;中断;
案例“gif”:$ctype=“image/gif”;中断;
案例“png”:$ctype=“image/png”;中断;
案例“wav”:$ctype=“audio/wav”;中断;
案例“mp3”:$ctype=“audio/mpeg3”;中断;
案例“wmv”:$ctype=“video/x-ms-wmv”;中断;
案例“avi”:$ctype=“video/avi”;中断;
案例“asf”:$ctype=“video/x-ms-asf”;中断;
案例“mpg”:$ctype=“video/mpeg”;中断;
案例“mpeg”:$ctype=“视频/mpeg”;中断;
案例“jpeg”:
案例“jpg”:$ctype=“image/jpg”;中断;
默认值:$ctype=“应用程序/强制下载”;
}
//一切都很顺利-如果需要,您可以将下载记录在数据库中?
//输出文件
页眉(“到期日期:1997年7月26日星期一05:00:00 GMT”);//缓存停止
标题(“上次修改:”.gmdate(“D,D M Y H:i:s”)“GMT”);//缓存停止
标题(“缓存控制:必须重新验证”);//缓存停止
标题(“内容类型:$ctype”);//输出文件类型
标题(“内容处置:附件;文件名=\”.basename($filename)。“\”;”;
标题(“内容传输编码:二进制”);
标题(“内容长度:“.filesize($fileDirectory.$filename));
readfile($fileDirectory.$filename);
退出();
保存文件的代码是什么?您确定文件已正确保存吗?在读取文件之前,您是否尝试将readfile()封装在is_readable(){…}中?@macjohn我刚刚注意到我的文件不可读。为什么不可以?如果文件名指定的文件或目录存在且可读,则is_readable()返回TRUE,否则返回FALSE。您的文件不存在,或者您没有读取该文件的权限。文件/目录现在可以读取,但仍然无法显示图片。@macjohn正在保存的文件大小正确,文件名正确,但由于某种原因不想打开。