download.php?id=1 点击之后下载文件$id  = isset($_REQUEST['id']) ? trim($_REQUEST['id']) : 0;
$file_path = 'http://www.********.com/file/';
$file_name = 'aaa_'.$id.'.rar';

if (file_exists($file_path . $file_name)){

header( "Pragma: public" );
header( "Expires: 0" );
header( 'Content-Encoding: none' );
header( "Cache-Control: must-revalidate, post-check=0, pre-check=0" );
header( "Cache-Control: public" );
header( "Content-type: application/octet-stream\n" );
header( "Content-Description: File Transfer" );
header( 'Content-Disposition: attachment; filename=' . $file_name );
header( "Content-Transfer-Encoding: binary" );
header( 'Content-Length: ' . filesize ( $file_path . $file_name ) );
readfile ( $file_path . $file_name );

} else {
echo '<script>alert("获取下载地址出错!")</script>';
echo '<script>location.replace("download.php");</script>';
exit;
}
这样可以使用浏览器另存为的方式下载,但如果是迅雷下载时显示下载文件为download.php这是怎么回事?如果是不弹出迅雷的下载窗口直接使用浏览器自带的下载方式改怎么写呢?有没有什么方法把文件存放的地址直接导向到迅雷的url下载地址?还有其它的下载文件的代码写法吗?

解决方案 »

  1.   

    我知道可以用这种方式直接下载<a href="http://www.******.com/file/aaa_1.rar">下载文件</a>但我的想法当是用户点击这个下载链接时,如果未登录跳转到登录页面,否则直接弹出下载窗口。所以我通过传递参数的方式到Php文件,先判断用户的登录状态再根据参数返回文件的地址。这样的话如果是迅雷下载就不能获取原始的文件存放地址了,出现了下载download.php的情况。
      

  2.   

    <a href="http://www.******.com/file/aaa_1.rar" onClick="checkLogin();">
    在onClick中检查登录。
      

  3.   


    请问怎么使用js检测用户登录状态,程序里面是使用session,js里怎么判断?
      

  4.   

    checkLogin中可以使用xmlhttp提交给php处理
      

  5.   

    最直接的if (file_exists($file_path . $file_name)){
    echo '<script>location.replace("'.$file_path . $file_name.'");</script>';
    exit;
    } else {
     echo '<script>alert("获取下载地址出错!")</script>';
     echo '<script>location.replace("download.php");</script>';
     exit;
    }
      

  6.   

    if (file_exists($file_path . $file_name)){
        echo 'location.replace("'.$file_path . $file_name.'");';
        exit;
    } else {
         echo 'alert("获取下载地址出错!");';
         echo 'location.replace("download.php");';
         exit;
    }下载时用ajax请求,并在js里eval返回结果
      

  7.   

    file_exists($filepath.$filename){
    header("Content-length:".filesize($filename));
     header( 'Content-Disposition: attachment; filename=' . $file_name );}