How php limits download speed
/**
- Desc: restrict download speed
- Link: bbs.it-home.org
- Date:
- */
- // Local file that shocould be sent to the client
- $ Local_file = 'test-file.zip ';
// Filename that the user gets as default
- $ Download_file = 'your-download-name.zip ';
// Set the download rate limit (=> 20, 5 kb/s)
- $ Download_rate = 20.5;
If (file_exists ($ local_file) & is_file ($ local_file )){
- // Send headers
- Header ('cache-control: private ');
- Header ('content-Type: application/octet-stream ');
- Header ('content-Length: '. filesize ($ local_file ));
- Header ('content-Disposition: filename = '. $ download_file );
// Flush content
- Flush ();
// Open file stream
- $ File = fopen ($ local_file, "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
- Sleep (1 );
- }
- // Close file stream
- Fclose ($ file );
- }
- Else {
- Die ('Error: The file'. $ local_file. 'does not exist! ');
- }
- ?>
Appendix: modify the Discuz Forum to speed up attachment download I have studied the Discuz code and implemented a feasible solution. Very simple: find attachment. pnp and find the bottom: function getlocalfile ($ filename, $ readmod = 1, $ range = 0) To:
Function getlocalfile ($ filename, $ readmod = 1, $ range = 0 ){
- If ($ fp = @ fopen ($ filename, 'RB ')){
- @ Fseek ($ fp, $ range );
- $ Download_rate = 10; // limits the network speed to 10 kb/s
- While (! Feof ($ fp ))
- {
- Print fread ($ fp, round ($ download_rate * 1024 ));
- Flush ();
- Ob_flush ();
- Sleep (1 );
- }
- }
- @ Fclose ($ fp );
@ Flush (); @ ob_flush ();
- }
- ?>
-
After testing, we found that the effect was quite obvious. |