PHP stops reading deleted files when it is fully loaded

I get a 30 second timeout error because the code continues to check if the file exceeds more than 5 MB when it is located. The code is designed to reject 5 MB files, but I need to also stop it when the file is under 5 MB. Is there a way to check the file transfer unit to make sure it is empty? I am currently using this DaveRandom example:

PHP Stop deleted file if it exceeds 5 mb

DaveRandom Code:

$url = 'http://www.spacetelescope.org/static/archives/images/large/heic0601a.jpg'; $file = '../temp/test.jpg'; $limit = 5 * 1024 * 1024; // 5MB if (!$rfp = fopen($url, 'r')) { // error, could not open remote file } if (!$lfp = fopen($file, 'w')) { // error, could not open local file } // Check the content-length for exceeding the limit foreach ($http_response_header as $header) { if (preg_match('/^\s*content-length\s*:\s*(\d+)\s*$/', $header, $matches)) { if ($matches[1] > $limit) { // error, file too large } } } $downloaded = 0; while ($downloaded < $limit) { $chunk = fread($rfp, 8192); fwrite($lfp, $chunk); $downloaded += strlen($chunk); } if ($downloaded > $limit) { // error, file too large unlink($file); // delete local data } else { // success } 
+6
source share
1 answer

You should check if you have reached the end of the file:

 while (!feof($rfp) && $downloaded < $limit) { $chunk = fread($rfp, 8192); fwrite($lfp, $chunk); $downloaded += strlen($chunk); } 
+5
source

All Articles