Home  >  Article  >  Backend Development  >  Download the PHP implementation file

Download the PHP implementation file

WBOY
WBOYOriginal
2016-08-08 09:25:531034browse

To download files in PHP, you first need to send some identification information to the Apache server through the header() function, telling Apache the path, name, type and other information of the file to be downloaded, and finally use the file reading and writing functions to read the file content and output it. .
Let’s look at an example:
$file = 'images/test.jpg';
if(is_file($file)) {
header("Content-Type: application/octet-stream");
        header("Content-Disposition: attachment; filename=".basename($file));
          ob_clean();
            readfile($file);
        exit; exit;
}
?>
The running results are as follows:


Note:
1. When the file is a binary stream and the downloaded file type is not known, the Content-Type uses application/octet-stream2. ob_clean() The function is to clear the output buffer. If this function is not used, the photo cannot be opened normally after the file is downloaded.
The above introduces the download of PHP implementation files, including the relevant content. I hope it will be helpful to friends who are interested in PHP tutorials.

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
Previous article:PHP redirectNext article:PHP redirect