PHP 下载文件到浏览器

方法一

a标签H5属性download属性,下载test.txt文件

方法二

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 下载文件到浏览器)