下面代码,下载下来的问题为什么大小只有0字节?if( $attachment['attachment'] )
{
$file = 'uploads/resource/'.$attachment['attachment'];
if( file_exists( dirname($file) ) )
{
$fileName = basename( $file );
header("Content-type: application/force-download");
header("Content-type: application/octet-stream");
header("Accept-Ranges: bytes");
header("Accept-Length: ".filesize( $file ));
header("Content-Disposition: attachment; filename=" . $fileName);
readfile( $file );
}
}
{
$file = 'uploads/resource/'.$attachment['attachment'];
if( file_exists( dirname($file) ) )
{
$fileName = basename( $file );
header("Content-type: application/force-download");
header("Content-type: application/octet-stream");
header("Accept-Ranges: bytes");
header("Accept-Length: ".filesize( $file ));
header("Content-Disposition: attachment; filename=" . $fileName);
readfile( $file );
}
}
你判断的是目录是否存在,而不是文件是否存在,因此如果文件确实不存在,下载的内容就可能是空
如果改为:if( file_exists( $file) ),也不行。
$file_name = $attachment['attachment'];
$file = $file_dir . $file_name;
这样提示文件没找到,但是我直接打到浏览器又可以,比如$file='123.docx';
127.0.0.1/webfolder/hz/uploads/resource/123.docx 这样下载下来是没问题的。我的PHP文件和保存文件的文件夹都在hz文件夹里。。
$file_name = $attachment['attachment'];
$file = $file_dir . $file_name;
这样提示文件没找到,但是我直接打到浏览器又可以,比如$file='123.docx';
127.0.0.1/webfolder/hz/uploads/resource/123.docx 这样下载下来是没问题的。echo $file;//试试看
那么你的程序在哪里?(echo __FILE__; 看一下)
D:\Code\webfolder\hz\public_attach_down.php
也就是说文档为
D:/Code/webfolder/hz/uploads/resource/123.docx
其中 D:/Code 为网站的根
你先
$file = 'D:/Code/webfolder/hz/uploads/resource/123.docx';
if( file_exists( $file ) ) {
$fileName = basename( $file );
header("Content-type: application/force-download");
header("Content-type: application/octet-stream");
header("Accept-Ranges: bytes");
header("Accept-Length: ".filesize( $file ));
header("Content-Disposition: attachment; filename=" . $fileName);
readfile( $file );
}测试一下
不行,没执行if里面的程序,难道是在iframe里不行?
你怎么总是说是 webfolder 呢?
红色部分只是个例子,代码中我是写htdocs_33594_v6这个的
if( file_exists( $file ) ) {
$fileName = basename( $file );
header("Content-type: application/force-download");
header("Content-type: application/octet-stream");
header("Accept-Ranges: bytes");
header("Accept-Length: ".filesize( $file ));
header("Content-Disposition: attachment; filename=" . $fileName);
readfile( $file );
}
这样在单独文件可以下载并正常,但是在iframe里就不行了,怪哉!!!!!