Oui, mais dans le lien la vérification à une autre fonction : afficher ou non l'icone de download (si une image .png existe dans le dossier). Elle n'est pas liée à la sécurité.
Mais ok, c'est bien ce que je pensais, c'est dangereux...
J'ai trouvé un autre script plus sécurisant qui peut m'aider à comprendre, mais qui a pour moi deux problèmes :
-Il ne fonctionne pas du internet explorer
-il ne permet pas de choisir le dossier par variable --> et j'ai peur de mettre un simple $filepath = $_GET["filepath"];
Voici ce script :
<?php
/** path du répertoir contenant les fichiers (à éditer) **/
define('DOWNLOAD_PATH', dirname(__FILE__).DIRECTORY_SEPARATOR.'photos-arbres'.DIRECTORY_SEPARATOR);
/** on récupère le nom du fichier demandé **/
if(isset($_GET['file']))
$file = $_GET['file'];
elseif(isset($_POST['file']))
$file = $_POST['file'];
else
$file = '';
/** tant qu'a faire évitons les attaques par directory transversal **/
$file = str_replace(array('../','..\\'),'',$file);
/** la variable est elle vide, le fichier existe ? **/
if(empty($file))
exit('Please select a file for download !');
elseif(!is_file(DOWNLOAD_PATH.$file))
exit('Requested file not found !');
function getMimeType($file)
{
$mimes = array(
'hqx' => 'application/mac-binhex40',
'doc' => 'application/msword',
'dot' => 'application/msword',
'bin' => 'application/octet-stream',
'lha' => 'application/octet-stream',
'lzh' => 'application/octet-stream',
'exe' => 'application/octet-stream',
'class' => 'application/octet-stream',
'so' => 'application/octet-stream',
'dll' => 'application/octet-stream',
'pdf' => 'application/pdf',
'ai' => 'application/postscript',
'eps' => 'application/postscript',
'ps' => 'application/postscript',
'smi' => 'application/smil',
'smil' => 'application/smil',
'wbxml' => 'application/vnd.wap.wbxml',
'wmlc' => 'application/vnd.wap.wmlc',
'wmlsc' => 'application/vnd.wap.wmlscriptc',
'xla' => 'application/vnd.ms-excel',
'xls' => 'application/vnd.ms-excel',
'xlt' => 'application/vnd.ms-excel',
'ppt' => 'application/vnd.ms-powerpoint',
'csh' => 'application/x-csh',
'dcr' => 'application/x-director',
'dir' => 'application/x-director',
'dxr' => 'application/x-director',
'spl' => 'application/x-futuresplash',
'gtar' => 'application/x-gtar',
'php' => 'application/x-httpd-php',
'php3' => 'application/x-httpd-php',
'php5' => 'application/x-httpd-php',
'phtml' => 'application/x-httpd-php',
'js' => 'application/x-javascript',
'sh' => 'application/x-sh',
'swf' => 'application/x-shockwave-flash',
'sit' => 'application/x-stuffit',
'tar' => 'application/x-tar',
'tcl' => 'application/x-tcl',
'xhtml' => 'application/xhtml+xml',
'xht' => 'application/xhtml+xml',
'xhtml' => 'application/xml',
'ent' => 'application/xml-external-parsed-entity',
'dtd' => 'application/xml-dtd',
'mod' => 'application/xml-dtd',
'gz' => 'application/x-gzip',
'zip' => 'application/zip',
'au' => 'audio/basic',
'snd' => 'audio/basic',
'mid' => 'audio/midi',
'midi' => 'audio/midi',
'kar' => 'audio/midi',
'mp1' => 'audio/mpeg',
'mp2' => 'audio/mpeg',
'mp3' => 'audio/mpeg',
'aif' => 'audio/x-aiff',
'aiff' => 'audio/x-aiff',
'm3u' => 'audio/x-mpegurl',
'ram' => 'audio/x-pn-realaudio',
'rm' => 'audio/x-pn-realaudio',
'rpm' => 'audio/x-pn-realaudio-plugin',
'ra' => 'audio/x-realaudio',
'wav' => 'audio/x-wav',
'bmp' => 'image/bmp',
'gif' => 'image/gif',
'jpeg' => 'image/jpeg',
'jpg' => 'image/jpeg',
'jpe' => 'image/jpeg',
'png' => 'image/png',
'tiff' => 'image/tiff',
'tif' => 'image/tif',
'wbmp' => 'image/vnd.wap.wbmp',
'pnm' => 'image/x-portable-anymap',
'pbm' => 'image/x-portable-bitmap',
'pgm' => 'image/x-portable-graymap',
'ppm' => 'image/x-portable-pixmap',
'xbm' => 'image/x-xbitmap',
'xpm' => 'image/x-xpixmap',
'ics' => 'text/calendar',
'ifb' => 'text/calendar',
'css' => 'text/css',
'html' => 'text/html',
'htm' => 'text/html',
'asc' => 'text/plain',
'txt' => 'text/plain',
'rtf' => 'text/rtf',
'sgml' => 'text/x-sgml',
'sgm' => 'text/x-sgml',
'tsv' => 'text/tab-seperated-values',
'wml' => 'text/vnd.wap.wml',
'wmls' => 'text/vnd.wap.wmlscript',
'xsl' => 'text/xml',
'mpeg' => 'video/mpeg',
'mpg' => 'video/mpeg',
'mpe' => 'video/mpeg',
'qt' => 'video/quicktime',
'mov' => 'video/quicktime',
'avi' => 'video/x-msvideo',
);
if(false === ($pos = strrpos($file,'.')))
return isset($mimes[$file]) ? $mimes[$file] : 'application/force-download';
$ext = substr($file,$pos+1);
return isset($mimes[$ext]) ? $mimes[$ext] : 'application/force-download';
}
/**** on désactive les erreurs ****/
error_reporting(0);
/**** on désactive la compression en sortie ****/
ini_set('zlib.output_compression', 0);
$now = time();
/*** Gestion du cache ***/
header('Pragma: public');
header('Last-Modified: '.gmdate("D, d M Y H:i:s").' GMT');
header('Cache-Control: must-revalidate, pre-check=0, post-check=0, max-age=0');
/**** Informations sur la réponse HTTP elle-même ****/
header('Date: '.gmdate("D, d M Y H:i:s", $now).' GMT');
header('Expires: '.gmdate("D, d M Y H:i:s", $now+1).' GMT');
header('Last-Modified: '.gmdate("D, d M Y H:i:s", $now).' GMT');
/**** Informations sur le contenu à envoyer ****/
header('Content-Tranfer-Encoding: none');
header('Content-Length: '.filesize(DOWNLOAD_PATH.$file));
header('Content-Type: '.getMimeType($file).'; name="'.$file.'"');
header('Content-Disposition: attachement; filename="'.$file.'"');
/**** pour finir lecture du fichier ****/
readfile(DOWNLOAD_PATH.$file);
exit();
?>