Home  >  Article  >  Backend Development  >  How to use PHP to implement the download function

How to use PHP to implement the download function

不言
不言Original
2018-07-24 09:27:153434browse

The content shared with you in this article is about how to use PHP to implement the download function. The content is of great reference value and I hope it can help friends in need.

PHP realizes simple download

PHPThe file is download.php, and the file for download is 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);    //关闭文件
?>

Visit and verify:
How to use PHP to implement the download function
How to use PHP to implement the download function

Related recommendations:

Methods for php file permissions when executing under Linux What?

How to use php to automatically load class files? Specific implementation plan (code) for php to automatically load base class files

The above is the detailed content of How to use PHP to implement the download function. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn