Here the code, now give me your opinion
What it does:
it get an argument as get request (file), it then use this argument to get a file-path, add one to the number on a txt file witch count the request for that file, and finally let's the client download the file (note: the page IS the file, as you can see by the modified header).
Why this?
This system use lock on file, so it will work even if you have parallel request. Also the client cannot find the real path of the file (well ok, vbrute force is the folder is exposed on the web), so he can't bypass the system.
This is helpful in free-host that give you no DB and no access to server's log/statistic.
Enjoy the code, and remember to give your opinion
(comment are in Italian, sorry)
<?php
/*
2013-03-10 12:00 UTC
Created by "lesto" from http://arduino.cc
*/
/*
PARTE CHE RICEVE IL NOME FILE, e che dovrei modificare
*/
if (!isset($_GET['file'])){
die('errore: nessun file indicato');
}
$nomeFile = '';
switch ($_GET['file']) {
case 'guida1':
$nomeFile = '0900766b811339e5.pdf';
break;
case 'guida2':
$nomeFile = 'file2.pdf';
break;
case 'guida3':
$nomeFile = 'file3.pdf';
break;
default:
die('errore: file indicato non trovato');
}
if (!file_exists($nomeFile) ){ //non contare o eseguire richieste a file che non esistono
die('errore: il file associato non esiste');
}
//scegliere uno dei seguenti
//$counter_file = 'contatore'.$nomeFile.'.txt'; //così conti il file
$counter_file = 'contatore'.$_GET['file'].'.txt'; //così conti il nome associato al file
/*
PARTE DEL CONTATORE UTENTI SISTEMATA X PARALLELISMO
*/
ignore_user_abort(true); //evita che la richiesta della pagina sia annullabile e quindi il file rimane bloccato o vuoto
if (file_exists($counter_file)) { //se il file esiste
$fh = fopen($counter_file, 'r+'); //aprilo in lettura + scrittura senza distruggere i dati
if (flock($fh, LOCK_EX)) { //se ottieni il lock sul file (è bloccante)
try{ //per essere certi che sia in caso di errore che no il file venga sbloccato
$buffer = trim(fread($fh, filesize($counter_file))); //leggi il file ed elimina gli spazi/caratteri strani ad inizio e fine
$buffer++; //incrementa il numero letto di 1
rewind($fh); //ritorna ad inizio file
fwrite($fh, $buffer); //scrivi il valore letto
ftruncate($fh, ftell($fh)); //elimina tutto nel file dalla posizione attuale in poi
fflush($fh); //forza la scrittura del file
}catch (Exception $e) {
echo 'Eccezione: ', $e->getMessage(), "\n"; //stampa l'errore
} //finally { // eseguito sia normalmente che sia in caso di errore
flock($fh, LOCK_UN); //rilascia il lock sul file
//}
}
}else { //se il file non esiste
$fh = fopen($counter_file, 'w+'); //crea il file vuoto
fwrite($fh, "1"); //scrivi 1
}
fclose($fh); //chiudi il file
/*
PARTE CHE TRASFORMA LA PAGINA IN UN FILE AGLI OCCHI DEL BROWSER
*/
$handle = fopen($nomeFile, "rb"); //apro il file in lettura modo binario
if (!$handle){ //se l'apertura del file fallisce
die('Errore aprendo il file');
}
$fileData = fread($handle, filesize($nomeFile)); //ne leggo TUTTO il contenuto
fclose($handle); //chiudo il file
header("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename=$nomeFile");
header('Content-Length: '.strlen($fileData));
header("Pragma: no-cache");
header("Expires: 0");
echo $fileData;
?>