简体   繁体   中英

php download limit zip damage when set file size

Zip damaged after set file size header

I have this code to limit download speed in php :

function readfile_chunked($filename, $retbytes = TRUE) {
 $download_rate = 85;  global $filename ;
  // send headers
  header('Cache-control: private');
  header('Content-Type: application/octet-stream');
  header('Content-Length: '.filesize($filename));
  header('Content-Disposition: filename=file.zip');

  // flush content
  flush();
  // open file stream
  $file = fopen($filename, "r");
  while(!feof($file)) {

      // send the current file part to the browser
      print fread($file, round($download_rate * 1024));

      // flush the content to the browser
      flush();

      // sleep one second
      usleep(200);
  }

}

When i set header for content length like this:

header('Content-Length: '.filesize($filename));

and open the downloaded file show me an alert that file is damaged . Not just the zip files i try on jpg file and its the same .

BUT WHEN I REMOVE THE FILE SIZE HEADER THE FILE OR IMAGE OPEN WITHOUT ANY ERROR

I #solved the problem by adding ob_clean() in the begin of function. I was trying to solve it for 3 months This need a party ;)

function party(forVisitors){
Drink(); 
Play();
HaveFun(); 
GoHome();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM