When developing a website or application, we often need to implement the file download function, which can be easily implemented through PHP. This article will introduce how to use PHP to implement the file download function.
1. File download process
Before downloading the file, we need to understand the download process:
2. Prepare to download the file
In PHP, we can use the readfile function to read the file content and output it to the browser. However, we need to make sure the file exists, otherwise an exception will be thrown. The following is a simple code example:
$file_path = '/path/to/your/file/yourfile.extension'; if (file_exists($file_path)) { header('Content-Type: application/octet-stream'); header('Content-Disposition: attachment; filename=' . basename($file_path)); header('Content-Transfer-Encoding: binary'); header('Content-Length: ' . filesize($file_path)); readfile($file_path); exit; } else { die('File not found.'); }
In the above code, we first use the file_exists function to check whether the file exists. If the file exists, set the HTTP header file to tell the browser that a file is about to be downloaded, and in the Content Specify the file name in -Disposition, specify the file type in Content-Type, and use the readfile function to output the file content.
3. Prevent file downloads from being stolen links
Due to the nature of the browser, some unscrupulous sites may steal your files, so we need to prevent file downloads from being stolen links.
We can add the following code to prevent file downloads from being stolen:
$referer = $_SERVER['HTTP_REFERER']; if ($referer && !preg_match('/^https?://' . $_SERVER['SERVER_NAME'] . '/', $referer)) { header("HTTP/1.1 403 Forbidden"); die("Access denied."); }
This code will check the HTTP_REFERER header information and ensure that it matches the domain name of the current server. If it does not match, it will return 403 Status code, access prohibited.
4. Implement segmented download
When downloading larger files, you may need to implement segmented download function to speed up the download speed and reduce network bandwidth usage. With the help of HTTP 1.1's Range header information, we can easily implement segmented downloads.
The following is a sample code:
$file_path = '/path/to/your/file/yourfile.extension'; if (file_exists($file_path)) { $size = filesize($file_path); $start = 0; $end = $size - 1; if (isset($_SERVER['HTTP_RANGE'])) { if (preg_match('/bytes=h*(d+)-(d*)[D.*]?/i', $_SERVER['HTTP_RANGE'], $matches)) { $start = intval($matches[1]); if (!empty($matches[2])) { $end = intval($matches[2]); } } } if ($start > $end || $end > $size - 1 || $start < 0) { header('HTTP/1.1 416 Requested Range Not Satisfiable'); header("Content-Range: bytes $start-$end/$size"); exit; } header('Content-Type: application/octet-stream'); header('Content-Disposition: attachment; filename=' . basename($file_path)); header('Content-Transfer-Encoding: binary'); header("Content-Range: bytes $start-$end/$size"); header('Accept-Ranges: bytes'); header('Content-Length: ' . ($end - $start + 1)); header("Cache-control: private"); header('Pragma: private'); header('Expires: ' . gmdate('D, d M Y H:i:s T', time() + 3600)); $fp = fopen($file_path, 'rb'); fseek($fp, $start); $buffer_size = 1024 * 8; //每次读取8 KB $bytes_send = 0; while (!feof($fp) && ($bytes_send < $end - $start + 1)) { $buffer = fread($fp, $buffer_size); echo $buffer; flush(); $bytes_send += strlen($buffer); } fclose($fp); exit; } else { die('File not found.'); }
In the above code, we first check the HTTP_RANGE header information. If the header information does not exist, the entire file is output. If the header information exists, the start offset and end offset are parsed, then the fseek function is used to locate the file pointer, and the fread function is used to read the file content and output it to the browser.
5. Conclusion
It is not difficult to implement the file download function in PHP. We only need to check whether the file exists and set the correct HTTP header information. Using HTTP 1.1 Range header information can easily achieve segmented downloads, and preventing file downloads from stolen links can protect the security of files. Therefore, we can choose to implement the required functions according to our needs to achieve a better user experience.
The above is the detailed content of Download file in PHP. For more information, please follow other related articles on the PHP Chinese website!