我有一个用PHP和Yii编写的Web应用程序,它允许用户使用swfupload上传图像和视频。有两种类型的用户:高级和常规。使用PHP或swfupload限制每个用户的上传速度
我想限制普通用户的上传速度,但我找不到任何不是全球的方式。
上传速度可以限制每个用户从PHP,Yii或swfupload?
我有一个用PHP和Yii编写的Web应用程序,它允许用户使用swfupload上传图像和视频。有两种类型的用户:高级和常规。使用PHP或swfupload限制每个用户的上传速度
我想限制普通用户的上传速度,但我找不到任何不是全球的方式。
上传速度可以限制每个用户从PHP,Yii或swfupload?
下载速度限制解决方案。您可以使用相同的上传。 .i.e.将文件读入较小的块,然后将每个块上传至睡眠状态:)
假设我们想限制/ images /文件夹中找到的图像的带宽。
首先,我们需要一个带宽php脚本,其中我们设置了下载速度的限制。我们做到这一点通过读取请求的文件小包,用读数之间超时:
<?php
//Enter the bandwidth here
$bandwidth = '32'; // KB/s
//For security reason, we will add here all the pattern that we allow for the filename
//Change this to your own needs
$allowed_file_patterns = array('/images\/(\w+)\.(jpg|gif|jpeg|png)/i');
function getMimeType($file_path)
{
$mtype = '';
if (function_exists('mime_content_type')){
$mtype = mime_content_type($file_path);
}
elseif (function_exists('finfo_file')){
$finfo = finfo_open(FILEINFO_MIME);
$mtype = finfo_file($finfo, $file_path);
finfo_close($finfo);
} elseif (function_exists('getimagesize')){
$finfo = @getimagesize($file_path);
//var_dump($finfo);
$mtype = !empty($finfo['mime'])?$finfo['mime']:'';
}
if ($mtype == ''){
$mtype = "application/force-download";
}
return $mtype;
}
$accepted_pattern = false;
foreach ($allowed_file_patterns as $pattern){
if (preg_match($pattern,$_GET['file'])){
$accepted_pattern = true;
}
}
if (!$accepted_pattern){
//Stop the script if is not a valid access
header("HTTP/1.1 403 Forbidden");
echo 'Forbidden request';
exit;
}
$fileName = $_GET['file'];
$fh = @fopen($fileName,'rb');
if (!$fh){
echo 'Unable to open file';
exit;
}
$fileSize = filesize($fileName);
header("Content-Type: ".getMimeType($fileName));
header("Content-Length: " . $fileSize);
while(!feof($fh))
{
//Read the allowed bandwidth, and then just wait a second
print(fread($fh, $bandwidth*1024));
usleep(1000000);
}
fclose($fh);
?>
现在,你可以创建一个.htaccess文件,你需要限制的所有请求,该带宽脚本重定向:
RewriteEngine on
RewriteCond %{REQUEST_URI} \.(gif|jpg|jpeg|png|GIF|JPG|JPEG|PNG)$
RewriteRule (.*) bandwidth.php?file=$1 [L]
如果你想要的话,你可以限制只有一些ip或一些referals的带宽。在htaccess文件,你将有:
RewriteEngine on
RewriteCond %{REMOTE_ADDR} ^123\.45\.6\.78$ [NC,OR]
RewriteCond %{REMOTE_ADDR} ^127\.0\.0 [NC]
RewriteCond %{REQUEST_URI} \.(gif|jpg|jpeg|png|GIF|JPG|JPEG|PNG)$
RewriteRule (.*) bandwidth.php?file=$1 [L]
RewriteCond %{HTTP_REFERER} ^http://(www\.)?php-code.net/.*$ [NC,OR]
RewriteCond %{HTTP_REFERER} ^http://(www\.)?example.com/.*$ [NC]
RewriteCond %{REQUEST_URI} \.(gif|jpg|jpeg|png|GIF|JPG|JPEG|PNG)$
RewriteRule (.*) bandwidth.php?file=$1 [L]
打算通过这种方式,可以限制从吸血网站的流量,没有禁止他们,我认为这是一个更好的解决方案。
有流php://input
这...
不可用ENCTYPE = “多部分/格式数据”。
如果你没有这个限制,你可以注册一个流过滤器并在它上面做一些令牌桶,与bandwidth-throttle/bandwidth-throttle
use bandwidthThrottle\BandwidthThrottle;
$in = fopen("php://input", "r");
$out = fopen(__DIR__ . "/upload.txt", "w");
$throttle = new BandwidthThrottle();
$throttle->setRate(100, BandwidthThrottle::KIBIBYTES); // Set limit to 100KiB/s
$throttle->throttle($in);
stream_copy_to_stream($in, $out);
欢呼@yoshi。这会给你想法 – shail
谢谢你,但我不需要限制下载速度,我需要限制**上传速度,每个用户,如果可能的话:) – yoshi
这是主意。使用相同的上传。将文件拆分为多个较小的文件。然后上传每个大块,并把睡眠/睡眠在那里 – shail