DedicatedChop
DedicatedChop

Reputation:

Limit download count with text file in PHP

if ($_SERVER['REQUEST_METHOD']=='GET' && $_GET['download']==='1')
{
    $handle = fopen('lastdownload.txt','rw');
    $date = @fread($handle,filesize('lastdownload.txt'));

    if (time() - 30 * 60 > $date)
    {
    fwrite($handle,time());
    header('Content-type: application/zip');
    header('Content-Disposition: attachment; filename="dwnld_'.date('d_m_Y_H_i',filemtime('download.zip')).'.zip"');
    readfile('download.zip');
    }

    exit;
}

Hi everyone, i have a problem about limiting download count.

I want to limit my download count.

If someone request the file with ?download=1

It checks the current time and the time inside the file

If 30 minutes passed before the last download, it lets you download again, else it just exits.

Any help please?

Thank you.

Upvotes: 1

Views: 1136

Answers (3)

Mahavir Nahata
Mahavir Nahata

Reputation: 86

if(fileatime("lastdownload.txt")>=300)
    {
    //Access OR File Download Code Here
    }

Upvotes: 0

Matteo Riva
Matteo Riva

Reputation: 25060

Unless your still using PHP4, I would just use file_put_contents() and file_get_contents().

Upvotes: 1

Heri
Heri

Reputation: 2124

"rw" is not a valid mode for fopen. You should use "r+" or "x+" and rewind the file pointer after reading:

$handle = fopen('lastdownload.txt','r+');
$date = @fread($handle,filesize('lastdownload.txt'));
rewind($handle);

Upvotes: 0

Related Questions