admin 管理员组

文章数量: 887021

原文地址:https://blog.csdn/qq_21233661/article/details/78807892

方法一

a标签H5属性download属性,该方法暂不兼容ie浏览器

<a download='test.txt' href='http://www.baidu'>下载test.txt文件</a>

方法二

PHP直接输出(亲测可用)

    ob_start();  
    $filename=$_GPC['url'];  
    $title=substr($filename,strrpos($filename,'/')+1);  
    $size=readfile($filename);  
    //var_dump($size);exit;  
    Header( "Content-type:application/octet-stream");  
    Header( "Accept-Ranges:bytes");  
    Header( "Accept-Length:");  
    header( "Content-Disposition:  attachment;  filename= $title");  
    //echo file_get_contents($size);  
    exit;

方法三

    $filename=$_GPC['url'];  
    $title=substr($filename,strrpos($filename,'/')+1);  
    $file  =  fopen($filename, "rb");  
    Header( "Content-type:  application/octet-stream ");  
    Header( "Accept-Ranges:  bytes ");  
    Header( "Content-Disposition:  attachment;  filename= $title");  
    $contents = "";  
    while (!feof($file)) {  
        $contents .= fread($file, 8192);  
    }  
    echo $contents;  
    fclose($file); 

本文标签: 浏览器 文件 php