Complete list of PHP file download functions: analysis of file download examples of readfile, header, Content-Disposition and other functions

PHPz
Release: 2023-11-18 15:28:02
Original
1614 people have browsed it

Complete list of PHP file download functions: analysis of file download examples of readfile, header, Content-Disposition and other functions

Comprehensive list of PHP file download functions: file download example analysis of readfile, header, Content-Disposition and other functions

File downloading is an essential function in Web applications One, and PHP, as a widely used Web development language, provides a variety of functions and methods for file downloading.

This article will introduce commonly used file download functions in PHP, including readfile, header, Content-Disposition, etc., and show corresponding code examples to help everyone better understand and master the implementation of file downloading.

1. readfile() function

The readfile() function is one of the most commonly used file download functions in PHP. It is used to read files and send them to the output stream. The following is a basic syntax example of using the readfile() function to download a file:

$file = 'example.txt';
if (file_exists($file)) {
    header('Content-Description: File Transfer');
    header('Content-Type: application/octet-stream');
    header('Content-Disposition: attachment; filename="'.basename($file).'"');
    header('Expires: 0');
    header('Cache-Control: must-revalidate');
    header('Pragma: public');
    header('Content-Length: ' . filesize($file));
    readfile($file);
    exit;
}
Copy after login

The above code first checks whether the file exists, then sets various parameters of the output stream, and finally uses the readfile() function to read and send the file. into the output stream. The advantage of using the readfile() function is that it is convenient and fast, and the amount of code is small. But the disadvantages are also obvious. This function will read the entire file into the memory at one time and then send it. If the file is too large, it may cause a server performance bottleneck.

2. Header() function

The header() function can be used to send HTTP headers. It is usually used in conjunction with the readfile() function to implement file downloading. The header() function can set various HTTP headers, including Content-Type, Content-Disposition, Content-Length, etc.

The following is an example of using the header() function and the readfile() function to implement file downloading:

$file = 'example.txt';
if (file_exists($file)) {
    header('Content-Description: File Transfer');
    header('Content-Type: application/octet-stream');
    header('Content-Disposition: attachment; filename="'.basename($file).'"');
    header('Expires: 0');
    header('Cache-Control: must-revalidate');
    header('Pragma: public');
    header('Content-Length: ' . filesize($file));
    ob_clean();
    flush();
    readfile($file);
    exit;
}
Copy after login

The above code is similar to the previous example code, the only difference is that ob_clean( ) and flush() functions. The ob_clean() function can clear the output buffer to ensure that response headers can be sent correctly. The flush() function can force all output to be sent to the client.

3. Content-Disposition

Content-Disposition is an HTTP header used to indicate how to handle the transmitted data, such as whether to download the file as an "attachment". By setting the Content-Disposition header we can specify the name of the file when downloading.

The following is an example of using the Content-Disposition header to implement file download:

$file = 'example.txt';
if (file_exists($file)) {
    header('Content-Description: File Transfer');
    header('Content-Type: application/octet-stream');
    header('Content-Disposition: attachment; filename="example.txt"');
    header('Expires: 0');
    header('Cache-Control: must-revalidate');
    header('Pragma: public');
    header('Content-Length: ' . filesize($file));
    ob_clean();
    flush();
    readfile($file);
    exit;
}
Copy after login

Compared with the previous sample code, this time we specify the file name in the Content-Disposition header , without using the basename() function to extract the file name from the file path. It should be noted that the quotes in filename="example.txt" are required, otherwise the browser may cause parsing errors when the file name contains spaces.

Other Notes

When using the file download function, we also need to pay attention to the following points:

1. The file path must be a relative path or an absolute path, not URL, otherwise the download will fail.

2. When using the header() function to set the HTTP header, it must be called before outputting any content.

3. Adding ob_clean() and flush() functions can avoid the problem of failure to send certain response headers.

4. If the downloaded file is very large, you should consider downloading in segments or using other download optimization methods.

Summary

PHP file download functions mainly include readfile, header, Content-Disposition, etc. Using these functions can quickly and easily implement the file download function, but you need to pay attention to issues such as file paths, HTTP header settings, buffer cleaning, etc. to ensure that the download can proceed normally. For large files, download performance and efficiency issues also need to be considered.

The above is the detailed content of Complete list of PHP file download functions: analysis of file download examples of readfile, header, Content-Disposition and other functions. For more information, please follow other related articles on the PHP Chinese website!

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!