You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

求助:为已购PHP文件管理器添加用户每日下载次数限制

Adding Daily Download Limit to Your PHP File Manager

Hey there! I see you need to add a daily download limit feature to your PHP file manager, and since you're solid on HTML but new to PHP, let's break this down step by step. We'll extend the existing Downloader class you provided to track and enforce daily download counts per user.

Step 1: Add a Daily Limit Check Method

First, we'll add a new method to the Downloader class that checks if the user has exceeded their daily download quota. This method will handle resetting counts for new days, checking against the limit, and updating the user's download stats.

Add this inside the Downloader class (place it before the download method for organization):

/**
 * Check if user has exceeded their daily download limit
 *
 * @return bool True if download is allowed, false if limit is reached
 */
public function checkDailyDownloadLimit() {
    global $gateKeeper, $setUp;
    
    // Fetch existing user download stats (default to 0 if not set)
    $dailyDownloads = (int)$gateKeeper->getUserInfo('daily_downloads') ?: 0;
    $lastDownloadDate = $gateKeeper->getUserInfo('last_download_date') ?: date('Y-m-d');
    
    // Reset download count if today is a new day
    if ($lastDownloadDate !== date('Y-m-d')) {
        $dailyDownloads = 0;
        $gateKeeper->updateUserInfo('last_download_date', date('Y-m-d'));
    }
    
    // Get daily limit from config (fallback to 5 if not configured)
    $dailyLimit = (int)$setUp->getConfig('daily_download_limit') ?: 5;
    
    // Block download if limit is reached
    if ($dailyDownloads >= $dailyLimit) {
        return false;
    }
    
    // Increment count and save to user info
    $gateKeeper->updateUserInfo('daily_downloads', $dailyDownloads + 1);
    return true;
}

Step 2: Enforce the Limit in the Download Method

Now we need to trigger this check before allowing any download. Modify the download method to run the limit check first—if the user has hit their quota, we'll send a 403 Forbidden response and stop the download process.

Update the download method like this:

public function download( $file, $filename, $file_size, $content_type, $disposition = 'inline', $android = false ) {
    // Check daily download limit before proceeding
    if (!$this->checkDailyDownloadLimit()) {
        header("HTTP/1.1 403 Forbidden");
        echo "Sorry, you've reached your daily download limit. Please try again tomorrow.";
        // You can replace the plain text above with custom HTML to match your manager's design
        return false;
    }

    // Rest of the existing download code remains unchanged
    // Gzip enabled may set the wrong file size.
    if (function_exists('apache_setenv')) {
        @apache_setenv('no-gzip', 1);
    }
    if (ini_get('zlib.output_compression')) {
        @ini_set('zlib.output_compression', 'Off');
    }
    @set_time_limit(0);
    session_write_close();
    header("Content-Length: ".$file_size);
    if ($android) {
        header("Content-Type: application/octet-stream");
        header("Content-Disposition: attachment; filename=\"".$filename."\"");
    } else {
        header("Content-Type: $content_type");
        header("Content-Disposition: $disposition; filename=\"".$filename."\"");
        header("Content-Transfer-Encoding: binary");
        header("Expires: -1");
    }
    if (ob_get_level()) {
        ob_end_clean();
    }
    readfile($file);
    return true;
}

To make the limit adjustable without editing code, add a new setting to your file manager's configuration file (usually located in etc/config.php):

// Add this line to set your desired daily download limit (change 5 to any number)
$setUp->setConfig('daily_download_limit', 5);

Quick Notes for Your Setup

  • User Info Storage: This code assumes your $gateKeeper class has an updateUserInfo method to save download stats. If it doesn't, adjust this part to match how user data is stored (e.g., using $_SESSION for session-based users, or direct database queries if user data is in a DB).
  • Anonymous Users: If you need to limit unlogged users, track counts using their IP address stored in a session or database instead of user-specific info.
  • Error UI: Since you're comfortable with HTML, feel free to replace the plain error message with a styled HTML template that fits your file manager's look.

That should implement the daily download limit you need! This setup will automatically reset counts at midnight each day and block downloads once the quota is hit.

内容的提问来源于stack exchange,提问作者Balder Bauer

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.13 06:38:22