PHP 调用浏览器下载文件

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

方法一

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

<a download='test.txt' href='http://www.baidu.com'>下载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实用方法,php,文件下载)