1. Problem: When PHP uses the readfile function to define the file download, the file must not be too large, otherwise the download will fail, the file will be damaged and no error will be reported;
2. Cause: This is because readFile will put the file into the cache when reading the file, resulting in memory overflow.
3. Solution: section download, and limit the download speed;
<? PHP // Set the maximum file execution time set_time_limit(0); if (isset($_GET['filename']) && ! empty($_GET['filename'])) { $file_name = $_GET['filename']; $file = __DIR__ . '/assets/' . $file_name; } else { echo 'what are your searching for? '; exit(); } if (file_exists($file) && is_file($file)) { $filesize = filesize($file); header('Content-Description: File Transfer'); header('Content-Type: application/octet-stream'); header('Content-Transfer-Encoding: binary'); header('Accept-Ranges: bytes'); header('Expires: 0'); header('Cache-Control: must-revalidate'); header('Pragma: public'); header('Content-Length: ' . $filesize); header('Content-Disposition: attachment; filename=' . $file_name); $fp = fopen($file, 'rb'); // set pointer position fseek($fp, 0); // enable buffer ob_start(); While (! feof($fp)) { $chunk_size = 1024 * 1024 * 2; // 2MB echo fread($fp, $chunk_size); ob_flush(); // Flush the PHP buffer to the Web server flush(); // Refresh the Web server buffer to the browser sleep(1); } // turn off the buffer ob_end_clean(); fclose($fp); } else { echo 'file not exists or has been removed! '; } exit();Copy the code