Bonjour, je vous explique mon probléme, alors j'ai trouver sur internet un script php pour comptabiliser les téléchargements.
Comme ca, il sufit de taper telecharger.php?fichier=icilenomdufichier.wmv
Le script va chercher les fichiers dans un dossier choisis au préalable. Mais seulement le problème est celui-ci :
Si quelqu'un de mal intentioné veut télécharger mes fichiers php dans d'autres répertoires il lui suffit de faire ceci par exemple
telecharger.php?fichier=../../index.php
Je ne sais pas quoi changer dans le code pour empecher ça, je suis vraiment embeté par ceci. Si quelqu'un a une idée de comment faire ça serait très simpa
Voici le code source de mon fichier telecharger.php
### Script configuration
$conf['host'] = 'mmmmmmmmmm';
$conf['base'] = 'mmmmmmm';
$conf['login'] = 'mmmmm';
$conf['password'] = 'mmmm';
// Path of the files;
$path = "/homepages/htdocs/downloads/";
function download($chemin,$fichier){
$length = filesize("$chemin/$fichier");
header("Content-Type: application/force-download; name=\"$fichier\"");
header("Content-Transfer-Encoding: binary");
header("Content-Length: $length");
header("Content-Disposition: attachment; filename=\"$fichier\"");
header("Expires: 0");
header("Cache-Control: no-cache, must-revalidate");
header("Pragma: no-cache");
readfile("$chemin/$fichier");
}
function err_msg($text) {
$out = '<center>'."\n";
$out .= '<br><br><br>'."\n";
$out .= '<font face="Arial, Helvetica" color="#D20000" size="2"><b>'.$text.'</b></font>'."\n";
$out .= '</center>';
die($out);
}
$file = ( isset($_GET['file']) && $_GET['file']!='' ) ? $_GET['file'] : null;
$request = "select * FROM files WHERE nom LIKE '$file'";
if(!$file){
err_msg('Spécifiez un fichier <br>('.$_SERVER['PHP_SELF'].'?file=monfichier)');
}elseif(!is_file($path.$file)){
err_msg('Fichier introuvable');
}
$date = date("Y-m-d H:i:s");
### Is the file already in the db ?
$db = mysql_connect($conf['host'], $conf['login'], $conf['password']);
mysql_select_db($conf['base'],$db);
$req = mysql_query($request);
$in_db = @mysql_num_rows($req);
### The file has not been downloaded yet
if(!$in_db){
$sql = "INSERT INTO files (ID, nom, downloads, date) Values ('', '$file', '1', '$date')";
### Already downloaded
}else{
$records = mysql_fetch_array($req);
$rec_download = ($records['downloads']+1);
$sql = "UPDATE files SET downloads='$rec_download', date='$date' WHERE nom='$file'";
}
$db = mysql_connect($conf['host'], $conf['login'], $conf['password']);
mysql_select_db($conf['base'],$db);
$result = mysql_query($sql);
### Send file
download($path,$file);
mysql_close();