1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
|
<?php
// Ce download est lancé à partir d'un lien tel que:
// <a href="download.php?filename=fileToPrint&dossier=dossierToPrint">Télécharger fichier</a>
// ou header("Location: http://$host$uri/"."download.php?filename=fileToPrint&dossier=dossierToPrint");
// Récupération nom de fichier et chemin).
if( isset( $_GET['filename'] ) ) $filename = $_GET['filename'];
if( isset( $_GET['dossier'] ) ) $dossier = $_GET['dossier'];
// Modification de l'application utilisée en fonction du type de fichier
$ext = strrchr( $filename, "." );
switch( $ext ) {
case ".zip": $type = "application/zip"; break;
case ".txt": $type = "text/plain"; break;
case ".pdf": $type = "application/pdf"; break;
default: $type = "application/octet-stream"; break;
}
// Constitution de l'header suivant le type
header("Content-Description: File Transfer");
header("Content-Type: $type\n");
header("Content-Transfer-Encoding: binary");
header("Content-disposition: attachment; filename=$filename");
header("Content-Length: ".filesize( $dossier.$filename ) );
// Lecture et Affichage
readfile( $dossier.$filename );
?> |