码迷,mamicode.com
首页 > Web开发 > 详细

PHP实现简单下载功能

时间:2018-07-23 11:01:12      阅读:199      评论:0      收藏:0      [点我收藏+]

标签:声明   fopen   技术   功能   accept   app   while   load   header   

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);    //关闭文件
?>

访问验证一下:
技术分享图片
技术分享图片

PHP实现简单下载功能

标签:声明   fopen   技术   功能   accept   app   while   load   header   

原文地址:https://www.cnblogs.com/Timesi/p/9353053.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!