下面代码,下载下来的问题为什么大小只有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 );
}
}

解决方案 »

  1.   

    确认 $attachment['attachment'] 有值,且文件存在。
      

  2.   

    header('Content-length: '.filesize($file));
      

  3.   

    if( file_exists( dirname($file) ) )
    你判断的是目录是否存在,而不是文件是否存在,因此如果文件确实不存在,下载的内容就可能是空
      

  4.   


    如果改为:if( file_exists( $file) ),也不行。
      

  5.   

    没有进入第二个if, 说明的你 uploads/resource/ 目录不存在,当然文件也不存在。
      

  6.   

    $file_dir = '/webfolder/hz/uploads/resource/';
    $file_name = $attachment['attachment'];
    $file = $file_dir . $file_name;
    这样提示文件没找到,但是我直接打到浏览器又可以,比如$file='123.docx';
    127.0.0.1/webfolder/hz/uploads/resource/123.docx 这样下载下来是没问题的。我的PHP文件和保存文件的文件夹都在hz文件夹里。。
      

  7.   

    $file_dir = '/webfolder/hz/uploads/resource/';这个是系统的根目录,不是网站的根目录,你两个概念搞混了
      

  8.   

    $file_dir = 'd:/cede/webfolder/hz/uploads/resource/';  这样也不行呢
      

  9.   

    $file_dir = '/webfolder/hz/uploads/resource/';
    $file_name = $attachment['attachment'];
    $file = $file_dir . $file_name;
    这样提示文件没找到,但是我直接打到浏览器又可以,比如$file='123.docx';
    127.0.0.1/webfolder/hz/uploads/resource/123.docx 这样下载下来是没问题的。echo $file;//试试看
      

  10.   

    echo $file得到/webfolder/hz/uploads/resource/123.docx
      

  11.   

    127.0.0.1/webfolder/hz/uploads/resource/123.docx 这样下载下来是没问题的。
    那么你的程序在哪里?(echo __FILE__; 看一下)
      

  12.   

    echo __FILE__;得到D:\Code\webfolder\hz\public_attach_down.php,路径正确
      

  13.   

    程序
    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 );
    }测试一下
      

  14.   


    不行,没执行if里面的程序,难道是在iframe里不行?
      

  15.   

    D:/Code/htdocs_33594_v6/hz/uploads/resource/123.docx 
    你怎么总是说是 webfolder 呢?
      

  16.   


    红色部分只是个例子,代码中我是写htdocs_33594_v6这个的
      

  17.   

    $file = 'D:/Code/htdocs_33594_v6/uploads/public/20130420071624_1761_上传_案.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 );
    }
    这样在单独文件可以下载并正常,但是在iframe里就不行了,怪哉!!!!!
      

  18.   

     header("Content-Transfer-Encoding: binary");