douqie6454 2013-08-28 01:33
浏览 40

为什么这个PHP下载脚本不起作用?

Here is a simple script I have written to limit downloads for users to one at a time (IE if they are downloading a file then they cannot download another one until they cancel the current download or it finishes).

ignore_user_abort(true);

$local_file = $_GET['filename'];
$download_file = explode("/", $local_file);
$download_file = $download_file[count($download_file) -1];

// set the download rate limit (value is in kilobytes per second
$download_rate = 100;
if(file_exists($local_file) && is_file($local_file)) {
    $ip = visitor_ip();
    if(!are_downloading($ip)) {
        header('Cache-control: private');
        header('Content-Type: application/octet-stream');
        header('Content-Length: '.filesize($local_file));
        header('Content-Disposition: filename='.$download_file);

        flush();

        $file = fopen($local_file, "r");
        log_downloader($ip);

        while(!feof($file)) {
            if (!connection_aborted()) {
            // 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);
        } else {
            break;
            }
        }

        clear_downloader($ip);      
        fclose($file);
    } else {
        die('<span style="color:#DDDDDD">Due to server limitations you may only download one file at a time. Please cancel or wait for your current download to finish before trying again. Click <a href="/simfiles">here</a> to return.</span>');
    }
} else {
    die('Error: The file '.$local_file.' does not exist!');
}

function visitor_ip() { 
    if(isset($_SERVER['HTTP_X_FORWARDED_FOR']))
        $TheIp=$_SERVER['HTTP_X_FORWARDED_FOR'];
    else $TheIp=$_SERVER['REMOTE_ADDR'];

    return trim($TheIp);
}

function are_downloading($ip) {
    $query = "select * from downloaders where ip_addr='$ip'";
    $result = mysql_query($query);

    $num_rows = mysql_num_rows($result);

    return $num_rows > 0; 
}

function log_downloader($ip) {
    $query = "insert into downloaders (ip_addr) values ('$ip')";
    $result = mysql_query($query);
}

function clear_downloader($ip) {
    $query = "delete from downloaders where ip_addr='$ip'";
    $result = mysql_query($query);
}

When I test it out, it works fine, but for a lot of people, their IP never gets cleared out of the database - even when they have finished downloading/cancelled a file. Why don't the IPs get deleted?

  • 写回答

1条回答 默认 最新

  • douzhi3667 2013-08-28 03:46
    关注

    The problem was that with big downloads the MySQL connection went away, I simply had to reconnect in the clear_downloader function and now it works fine.

    评论

报告相同问题?

悬赏问题

  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)