Forcer le téléchargement d'un fichier

hood042 Messages postés 8 Date d'inscription vendredi 20 avril 2007 Statut Membre Dernière intervention 1 juin 2007 - 31 mai 2007 à 21:19
bonnebouffe Messages postés 38 Date d'inscription lundi 15 septembre 2003 Statut Membre Dernière intervention 24 novembre 2008 - 27 mai 2008 à 05:50
Bonjour, je voudrais forcer le téléchargement d'un fichier comme sur ce site : http://dimi30a.olympe-network.com/free.php
Pouvez-vous m'aider ? Merci pour vos réponses.

4 réponses

vilhjalms Messages postés 591 Date d'inscription mercredi 20 juillet 2005 Statut Membre Dernière intervention 16 juillet 2009 2
31 mai 2007 à 22:39
Saell og blesuð

<?php

$filename = $_GET['file'];

// required for IE, otherwise Content-disposition is ignored
if(ini_get('zlib.output_compression'))
  ini_set('zlib.output_compression', 'Off');

$file_extension = strtolower(substr(strrchr($filename,"."),1));

if( $filename == "" )
{
  echo "<html><title>Force Download Script</title> ERROR: download file NOT SPECIFIED. USE force-download.php?file=filepath </html>";
  exit;
} elseif ( ! file_exists( $filename ) )
{
  echo "<html><title>Force Download Script</title>ERROR: File not found. USE force-download.php?file =filepath</html>";
  exit;
};
switch( $file_extension )
{
  case "pdf": $ctype="application/pdf"; break;
  case "exe": $ctype="application/octet-stream"; break;
  case "zip": $ctype="application/zip"; break;
  case "doc": $ctype="application/msword"; break;
  case "xls": $ctype="application/vnd.ms-excel"; break;
  case "ppt": $ctype="application/vnd.ms-powerpoint"; break;
  case "gif": $ctype="image/gif"; break;
  case "png": $ctype="image/png"; break;
  case "jpeg":
  case "jpg": $ctype="image/jpg"; break;
  default: $ctype="application/force-download";
}
header("Pragma: public"); // required
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: private",false); // required for certain browsers
header("Content-Type: $ctype");
// change, added quotes to allow spaces in filenames, by Rajkumar Singh
header("Content-Disposition: attachment; filename="".basename($filename)."";" );
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".filesize($filename));
readfile("$filename");
exit;

?>

Vilhjálms
Sigurðsdóttir aka Frëyjá
0
hood042 Messages postés 8 Date d'inscription vendredi 20 avril 2007 Statut Membre Dernière intervention 1 juin 2007
1 juin 2007 à 22:24
Merci mais ce n'est pas comme ça que je veux faire ! Moi je voulais faire comme ce site :
>> http://dimi30a.olympe-network.com/free.php <<
0
MasterOfQuebec Messages postés 66 Date d'inscription lundi 12 mars 2007 Statut Membre Dernière intervention 5 mai 2009
4 juin 2007 à 18:59
Bonjour,
Je ne l'ai pas testé mais je ne crois pas qu'il y à des erreurs.

Download.html :
<html>


<head>
<title>Download</title>
</head>




<form name= "form" method="post" action="find.php" enctype="multipart/form-data">
   Url du fichier :

  
</form>



</html>

Find.php :
<?php


$filename = $HTTP_POSTVARS['url'];


// required for IE, otherwise Content-disposition is ignored
if(ini_get('zlib.output_compression'))
  ini_set('zlib.output_compression', 'Off');


$file_extension = strtolower(substr(strrchr($filename,"."),1));
switch( $file_extension )
{
  case "pdf": $ctype="application/pdf"; break;
  case "exe": $ctype="application/octet-stream"; break;
  case "zip": $ctype="application/zip"; break;
  case "doc": $ctype="application/msword"; break;
  case "xls": $ctype="application/vnd.ms-excel"; break;
  case "ppt": $ctype="application/vnd.ms-powerpoint"; break;
  case "gif": $ctype="image/gif"; break;
  case "png": $ctype="image/png"; break;
  case "jpeg":
  case "jpg": $ctype="image/jpg"; break;
  default: $ctype="application/force-download";
}
header("Pragma: public"); // required
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: private",false); // required for certain browsers
header("Content-Type: $ctype");
// change, added quotes to allow spaces in filenames, by Rajkumar Singh
header("Content-Disposition: attachment; filename="".basename($filename)."";" );
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".filesize($filename));
readfile("$filename");
exit;


?>

Téléchargeable ici : =1&id=1180976057&name=Download%20Script&publisher=MasterOfQuebec http://www.rocksplash.com/home/?show=1&id=1180976057&name=Download%20Script&publisher=MasterOfQuebec
0
bonnebouffe Messages postés 38 Date d'inscription lundi 15 septembre 2003 Statut Membre Dernière intervention 24 novembre 2008
27 mai 2008 à 05:50
Bonjour,

Merci beaucoup pour ce bout de code, il m'a bien dépanné.

Une petite question cependant, il fonctionne parfaitement avec IE et FF sur ma machine de développement (Windows 2003) mais dès que je le mets chez mon provider, il ne fonctionne plus avec IE.

Vers quelle piste dois-je chercher ? Je tourne en rond, je deviens fou :-(

Merci d'avance,

Christophe
0
Rejoignez-nous