如何通过PHP技术设置文件下载速度限制?
- 内容介绍
- 文章标签
- 相关推荐
本文共计285个文字,预计阅读时间需要2分钟。
PHP实现限制文件下载速度,将文件发送到客户端本地的内容:
php限制文件下载速度并将文件发送到客户端本地的代码:
if (file_exists($local_file) && is_file($local_file)) { $file_size=filesize($local_file); header('Content-Type: video/mp4'); header('Content-Disposition: attachment; filename=' . $download_file . ''); header('Content-Length: ' . $file_size); header('Accept-Ranges: bytes'); header('Cache-Control: no-cache, no-store, must-revalidate'); header('Pragma: no-cache'); header('Expires: 0');
$fp=fopen($local_file, 'rb'); set_time_limit(0);
while (!feof($fp)) { $buffer=fread($fp, 1024); echo $buffer; flush();
sleep(1 / $download_rate); // 控制下载速度 }
fclose($fp);} else { http_response_code(404); echo 'File not found.';}?>
php实现限制文件下载速度// 将发送到客户端的本地文件 $local_file = 'test.mp4'; // 文件名 $download_file = 'your-download-name.mp4'; // 设置下载速率(=> 20,5 kb/s) $download_rate = 20.5; if(file_exists($local_file) && is_file($local_file)) { // 发送 headers header('Cache-control: private'); header('Content-Type: application/octet-stream'); header('Content-Length: '.filesize($local_file)); header('Content-Disposition: filename='.$download_file); // flush 内容 flush(); // 打开文件流 $file = fopen($local_file, "r"); while (!feof($file)) { // 设置文件最长执行时间 set_time_limit(0); // 发送当前部分文件给浏览者 print fread($file, round($download_rate * 1024)); // flush 内容输出到浏览器端 flush(); // 防止PHP或web服务器的缓存机制影响输出 ob_flush(); // 终端1秒后继续 sleep(1); } // 关闭文件流 fclose($file); } else { die('Error: 文件 '.$local_file.' 不存在!'); }
本文共计285个文字,预计阅读时间需要2分钟。
PHP实现限制文件下载速度,将文件发送到客户端本地的内容:
php限制文件下载速度并将文件发送到客户端本地的代码:
if (file_exists($local_file) && is_file($local_file)) { $file_size=filesize($local_file); header('Content-Type: video/mp4'); header('Content-Disposition: attachment; filename=' . $download_file . ''); header('Content-Length: ' . $file_size); header('Accept-Ranges: bytes'); header('Cache-Control: no-cache, no-store, must-revalidate'); header('Pragma: no-cache'); header('Expires: 0');
$fp=fopen($local_file, 'rb'); set_time_limit(0);
while (!feof($fp)) { $buffer=fread($fp, 1024); echo $buffer; flush();
sleep(1 / $download_rate); // 控制下载速度 }
fclose($fp);} else { http_response_code(404); echo 'File not found.';}?>
php实现限制文件下载速度// 将发送到客户端的本地文件 $local_file = 'test.mp4'; // 文件名 $download_file = 'your-download-name.mp4'; // 设置下载速率(=> 20,5 kb/s) $download_rate = 20.5; if(file_exists($local_file) && is_file($local_file)) { // 发送 headers header('Cache-control: private'); header('Content-Type: application/octet-stream'); header('Content-Length: '.filesize($local_file)); header('Content-Disposition: filename='.$download_file); // flush 内容 flush(); // 打开文件流 $file = fopen($local_file, "r"); while (!feof($file)) { // 设置文件最长执行时间 set_time_limit(0); // 发送当前部分文件给浏览者 print fread($file, round($download_rate * 1024)); // flush 内容输出到浏览器端 flush(); // 防止PHP或web服务器的缓存机制影响输出 ob_flush(); // 终端1秒后继续 sleep(1); } // 关闭文件流 fclose($file); } else { die('Error: 文件 '.$local_file.' 不存在!'); }

