簡體   English   中英

使用PHP或swfupload限制每個用戶的上傳速度

[英]Limit upload speed per user with PHP or swfupload

我有一個用PHP和Yii編寫的Web應用程序,允許用戶使用swfupload上傳圖像和視頻。 用戶有兩種類型:高級用戶和普通用戶。

我想限制普通用戶的上傳速度,但是找不到任何非全局的方式。

是否可以限制每個用戶從PHP,Yii或swfupload的上傳速度?

有流php://input

不適用於enctype =“ multipart / form-data”。

如果沒有該限制,則可以注冊一個流過濾器並對其進行一些令牌存儲,例如,使用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);

這是下載的速度限制解決方案。 您可以將其用於上傳。 .ieread將文件分成較小的塊,然后在睡眠時上傳每個塊:)

假設我們想限制在/ 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或某些引薦資源限制帶寬。 在htaccess文件中,您將具有:

RewriteEngine on

輸入您要限制以下的IP或IP類別,並在它們之間使用[OR]

所有其他ip都將直接訪問該url,而無需通過bandwidth.php

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]

輸入您要限制以下的IP或IP類別,並在它們之間使用[OR]

所有其他ip都將直接訪問該url,而無需通過bandwidth.php

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]

這樣,您可以限制來自leecher網站的訪問量,而不會禁止它們,我認為這是一個更優雅的解決方案。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM