0

PHP と Yii で記述された Web アプリケーションを使用して、ユーザーがswfuploadを使用して画像とビデオをアップロードできるようにしています。ユーザーには、プレミアムとレギュラーの 2 種類があります。

通常のユーザーのアップロード速度を制限したいのですが、グローバルではない方法が見つかりません。

PHP、Yii、または swfupload からユーザーごとにアップロード速度を制限できますか?

4

2 に答える 2

0

php://input</p>

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);
于 2015-08-08T22:31:36.860 に答える
-1

ダウンロードの速度制限ソリューションは次のとおりです。アップロードにも同じものを使用できます。.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 は、bandwidth.php を経由せずに URL に直接アクセスします。

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 は、bandwidth.php を経由せずに URL に直接アクセスします。

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]

このようにして、リーチャー サイトからのトラフィックを禁止することなく制限することができます。これはより洗練されたソリューションだと思います。

于 2012-09-06T12:13:31.323 に答える