Forcer le téléchargement

kyuubi6 -  
 zzuutt -
Bonjour,

je sais que la question à déjà été posée mais je n'arrive pas à résoudre mon problème. Je dois en fait forcer le téléchargement d'un fichier mais mon fichier peut être un pdf ou une image ou un txt.
Voici ce que j'ai fait :
<a href='../download.php?file=".$do['nom_fichier']."&chemin=".$do['lien']."' target='_blank'>".$do['nom']."</a>

Et voici mon fichier download.php
<?php
/* $file = $_GET["file"];

header("Content-Type: application/octet-stream");
//header("Content-Length: ".filesize($file));
header("Content-Disposition: attachment; filename=".$file);
header("Content-Type: application/force-download");
readfile($file); 
//header("Location:".$file); */

$Fichier_a_telecharger = $_GET['file'];
$chemin = $_GET['chemin'] ;

//echo $chemin."<br/>".$Fichier_a_telecharger ;

 switch(strrchr(basename($Fichier_a_telecharger), ".")) {

case ".gz": $type = "application/x-gzip"; break;
case ".tgz": $type = "application/x-gzip"; break;
case ".zip": $type = "application/zip"; break;
case ".pdf": $type = "application/pdf"; break;
case ".png": $type = "image/png"; break;
case ".gif": $type = "image/gif"; break;
case ".jpg": $type = "image/jpeg"; break;
case ".txt": $type = "text/plain"; break;
case ".htm": $type = "text/html"; break;
case ".html": $type = "text/html"; break;
default: $type = "application/octet-stream"; break;

}

header("Content-disposition: attachment; filename=".$Fichier_a_telecharger);
header('Content-Type: application/force-download');
//header("Content-Type: ".$type); 
header("Content-Transfer-Encoding: ".$type."\n"); // Surtout ne pas enlever le \n
header("Content-Length: ".filesize($chemin . $Fichier_a_telecharger));
header("Pragma: no-cache");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0, public");
header("Expires: 0");
readfile($chemin . $Fichier_a_telecharger);  



?>

si c'est un fichier txt ca marche mais si c'est un pdf par exemple il me dit que j'ai une erreur ou que le fichier est corrompu.

Merci de votre aide

A voir également:

2 réponses

Mopra-L Messages postés 146 Date d'inscription   Statut Membre Dernière intervention   6
 
Bonjour,

A priori, de ce que j'en vois, le problème doit se situer au niveau de ton switch, qui ne reparti pas bien, selon la valeur de l'extension de ton fichier, ce qui fait qu'il utilise toujours la valeur par défaut (donc "application/octet-stream"), ce qui n'est surement pas compatible avec un fichier PDF.


Essaye de remplacer ton default
default: $type = "application/octet-stream"; break;

par
default: $type = "application/pdf"; break;


et regarde si, cette fois, les fichiers PDF se téléchargent bien.

Si oui, il va falloir trouver un autre façon de choisir le type selon l'extension.

Si non... ben on verra après, ça ^^'




0
zzuutt
 
Bonjour,
je ne comprends pas ton script
tu as 2 header !
avant et apres le case, cela ne peut pas marcher !

<?php
$Fichier_a_telecharger = $_GET['file'];
$chemin = $_GET['chemin'] ;

switch(strrchr(basename($Fichier_a_telecharger), ".")) {

case ".gz": $type = "application/x-gzip"; break;
case ".tgz": $type = "application/x-gzip"; break;
case ".zip": $type = "application/zip"; break;
case ".pdf": $type = "application/pdf"; break;
case ".png": $type = "image/png"; break;
case ".gif": $type = "image/gif"; break;
case ".jpg": $type = "image/jpeg"; break;
case ".txt": $type = "text/plain"; break;
case ".htm": $type = "text/html"; break;
case ".html": $type = "text/html"; break;
default: $type = "application/octet-stream"; break;

}

header("Content-disposition: attachment; filename=".$Fichier_a_telecharger);

//correspond a ton type de fichier
header("Content-Type: ".$type); 

// dessous tu mets binaire ou ascii
header("Content-Transfer-Encoding: binary");

header("Content-Length: ".filesize($chemin . $Fichier_a_telecharger));
header("Pragma: no-cache");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0, public");
header("Expires: 0");
readfile($chemin . $Fichier_a_telecharger);  

?>


avec ce script modifie cela devrait marcher
0