PHP实现简单下载功能

PHP实现简单下载

PHP文件为download.php,供下载的文件为1.jpg.

<?php
    $filename="1.jpg";
    if(!file_exists($filename)){
        die("文件不存在");
    }                   //判断文件是否存在

    $fp =fopen($filename,"r");  //打开文件

    $file_size=filesize($filename);   //声明文件大小

    header("Content-type:application/octet-stream");
    header("Accept-Ranges:bytes");   //按字节大小返回
    header("Accept-Length:".$file_size);   //告诉浏览器文件大小
    header("Content-Disposition: attachment; filename=".$filename);  //下载框中文件的名字

    $buffer=1024;

    while(!feof($fp)){
        $data=fread($fp,$buffer);
    }            //判断文件是否下载完
    
    fclose($fp);    //关闭文件
?>

访问验证一下:

猜你喜欢

转载自www.cnblogs.com/Timesi/p/9353053.html