How to use PHP to implement the download function

不言
Release: 2023-04-03 11:16:02
Original
3388 people have browsed it

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

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!