Forcer le téléchargement

Résolu
nagor19 Messages postés 4 Date d'inscription vendredi 13 octobre 2006 Statut Membre Dernière intervention 7 décembre 2007 - 30 juil. 2007 à 12:11
nagor19 Messages postés 4 Date d'inscription vendredi 13 octobre 2006 Statut Membre Dernière intervention 7 décembre 2007 - 30 juil. 2007 à 15:29
Salut,

J'ai créer un code pour télécharger des fichiers (.jpg, .doc, .pdf et autres) et tout fonctionne à merveille ou presque. En effet, j'ai un problème avec les fichiers .txt. avant le dowload le contenu du fichier est tel qu'il doit être mais après le dowload le code de la page est ajouté à la fin de ce contenu!!!! Plutôt embêtant.

voici mon code: /*  *   Variables  ==
 *  ===========================*/
 $fileDir = '../fichiers/'.$ID_Doc.'/'; // le dossier contenant les fichiers à télécharger
... /*  *   traitement du download forcé  ==
 *  ===============================================*/
 if(isset($_GET['file'])){
  $file = $fileDir.$_GET['file'];
  if (file_exists($file)){
   switch(strrchr(basename($file), ".")){
    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-type: application/force-download");
     header('Content-Disposition: inline; filename="'.$file.'"');
     //header("Content-Transfer-Encoding: Binary");
     header("Content-Transfer-Encoding: $type\n");
     header("Content-length: ".filesize($file));
     header('Content-Type: application/octet-stream');
     header('Content-Disposition: attachment; filename="'.$_GET['file'].'"');
     readfile("$file");
  }
 }

Alors si quelqu'un peut m'aider, je lui en serai très reconnaissant.
Merci

4 réponses

cs_caviar Messages postés 329 Date d'inscription samedi 4 janvier 2003 Statut Membre Dernière intervention 29 mars 2015 2
30 juil. 2007 à 12:23
    le code html de la page ???
trop bizare comme truc ...
ton code à l'air pas mal ...
sinon tupeux toujours passer par un fopen pour lire et stocker le contenu du fichier puis le transférer ... un file_get_contents
http://fr.php.net/manual/fr/function.file-get-contents.php
@+
3
Evangun Messages postés 1980 Date d'inscription dimanche 20 février 2005 Statut Membre Dernière intervention 24 septembre 2012 4
30 juil. 2007 à 12:37
Hello,

il faut juste ne mettre aucun HTML sur la page où tu forces le download, car tu dis au télécharger tout ce qui vient après le header, donc le code HTML compris.
3
nagor19 Messages postés 4 Date d'inscription vendredi 13 octobre 2006 Statut Membre Dernière intervention 7 décembre 2007
30 juil. 2007 à 15:02
Ok je pense que j'ai pigé. Je vais de ce pas tester
0
nagor19 Messages postés 4 Date d'inscription vendredi 13 octobre 2006 Statut Membre Dernière intervention 7 décembre 2007
30 juil. 2007 à 15:29
Super ça y est voici ma solution:

un lien:
[download.php?dir='.$fileDir.'&dwn='.$fichier.' Télécharger]

une page dédiée au dowload:
dowload.php

<?php
if(isset($_GET['dwn']) && isset($_GET['dir'])){
 $dwn = $_GET['dwn'];
 $dir = $_GET['dir'];
 if (file_exists($dir.$dwn)){
  switch(strrchr(basename($dir.$dwn), ".")){
   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=$dwn");
  header("Content-Type: application/force-download");
  header("Content-Transfer-Encoding: $type\n");
  header("Content-Length: ".filesize($dir.$dwn));
  header("Pragma: no-cache");
  header("Cache-Control: must-revalidate, post-check=0, pre-check=0, public");
  header("Expires: 0");
  readfile($dir.$dwn);
 }
}
?>

Merci à tous
0
Rejoignez-nous