1. 程式人生 > >PHP 下載檔案到瀏覽器

PHP 下載檔案到瀏覽器

方法一

a標籤H5屬性download屬性,<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);