我有一個用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