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 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
|
if(isset($_GET['file'])){
//Décoder la chaîne de caratère qui contient le nom du fichier qui a été passé en paramètre dans le lien de la page (page.php?file=nom+du+fichier.pdf)
$filename = html_entity_decode(urldecode($_GET['file']));
//Chercher la chaîne de caractère possédant le document encodé en Base64
$file_query = 'SELECT document_encode FROM document WHERE document_id=' . $_GET['file_id'];
$file_result = $db->query($file_query);
$file_result_row = mysqli_fetch_assoc($file_result);
//Extraction de l'extension du fichier
$file_extension = strtolower(substr(strrchr($filename, "."), 1));
//Assignation de la chaine de caratère en Base64 représentant le fichier à une variable
$document_encode = $file_result_row['document_encode'];
$content_type = '';
//Trouver le bon Content-Type selon l'extension
switch ($file_extension) {
case "pdf":
$content_type = "application/pdf";
break;
case "doc":
$content_type = "application/msword";
break;
case "docx":
$content_type = "application/vnd.openxmlformats-officedocument.wordprocessingml.document";
break;
case "xls":
$content_type = "application/vnd.ms-excel";
break;
case "xlsx":
$content_type = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
break;
case "ppt":
$content_type = "application/vnd.ms-powerpoint";
break;
case "pptx":
$content_type = "application/vnd.openxmlformats-officedocument.presentationml.presentation";
break;
case "odt":
$content_type = "application/vnd.oasis.opendocument.text";
break;
case "csv":
$content_type = "text/csv";
break;
case "txt":
$content_type = "text/plain";
break;
case "png":
$content_type = "image/png";
break;
case "jpg":
$content_type = "image/jpeg";
break;
case "jpeg":
$content_type = "image/jpeg";
break;
}
header("'Content-Type:" . $content_type . "'");
header("Content-Disposition: attachment; filename=\"$filename\"");
print base64_decode($document_encode);
exit;
} |