2

ディレクトリからファイルをダウンロードして保護するこの素晴らしいスクリプトを見つけました。

http://www.gowondesigns.com/?page.getfile

そして、私もウェブサイトからこのコードを見ました:

// local file that should be send to the client
$local_file = 'test-file.zip';

// filename that the user gets as default
$download_file = 'your-download-name.zip';

// set the download rate limit (=> 20,5 kb/s)
$download_rate = 20.5;

if(file_exists($local_file) && is_file($local_file)) {


// send headers
header('Cache-control: private');
header('Content-Type: application/octet-stream');
header('Content-Length: '.filesize($local_file));
header('Content-Disposition: filename='.$download_file);

// flush content
flush();

// open file stream
$file = fopen($local_file, "r");

while (!feof($file)) {

    // send the current file part to the browser
    print fread($file, round($download_rate * 1024));

    // flush the content to the browser
    flush();

    // sleep one second
    sleep(1);
}

// close file stream
fclose($file);


}
else {
    die('Error: The file '.$local_file.' does not exist!');
}

どうすればそれらを組み合わせることができますか? getfile スクリプトを使用してダウンロード速度を追加するにはどうすればよいですか?

追加してみました:

while (!feof($file)) {

    // send the current file part to the browser
    print fread($file, round($download_rate * 1024));

    // flush the content to the browser
    flush();

    // sleep one second
    sleep(1);
}

しかし、 $file の代わりに $fd にするべきだと思いますが、良い結果は得られませんでした

私は何を間違っていますか?

4

2 に答える 2

1

あなたのコメントに基づいて-私はあなたが次のことを望んでいると思います:

// open file stream
$file = fopen($local_file, "r");

while (!feof($file)) {

    // send the current file part to the browser
    print fread($file, round($download_rate * 1024));

    // flush the content to the browser
    flush();

    // sleep one second
    sleep(1);
}

// close file stream
fclose($file);

ただし、ユーザーにファイルのダウンロードと速度制限を正常に促すのはスクリプト全体であることに注意してください。質問の最初のスクリプトの名前を download.php に変更し、それにリンクし<a href='download.php?id=1'>Download 1</a>ます (ファイル ID 1 がダウンロードされます)。

<?php

$file_id = $_GET['id'];

if($file_id == 1){
    // local file that should be send to the client
    $local_file = 'test-file.zip';
    // filename that the user gets as default
    $download_file = 'your-download-name.zip';
} else {
    die('Invalid file selected for download');
}

// set the download rate limit (=> 20,5 kb/s)
$download_rate = 20.5;

if(file_exists($local_file) && is_file($local_file)) {
    // send headers
    header('Cache-control: private');
    header('Content-Type: application/octet-stream');
    header('Content-Length: '.filesize($local_file));
    header('Content-Disposition: filename='.$download_file);

    // flush content
    flush();

    // open file stream
    $file = fopen($local_file, "r");

    while (!feof($file)) {
        // send the current file part to the browser
        print fread($file, round($download_rate * 1024));

        // flush the content to the browser
        flush();

        // sleep one second
        sleep(1);
    }

    // close file stream
    fclose($file);
} else {
    die('Error: The file '.$local_file.' does not exist!');
}
?>
于 2012-02-14T08:11:41.410 に答える