Pb Forcer un télechargement en PHP

arfal Messages postés 37 Date d'inscription   Statut Membre Dernière intervention   -  
 Pierre Crevoisier -
Bonjour tout le monde,
J'ai un problème pour forcer le télechargement d'un fichier Word.
Su le fichier HTML j'ai mis ce qui suit :

<a href="download.php?filname=firm_survey_en.doc">Click here to download the questionnaire file (If you like it as MS Word) </a>

Dans le fichier download.php j'ai mis ce qui suit :
<?php
header("Content-type: application/force-download");
header("Content-Disposition: attachment; filename=firm_survey_en.doc");
readfile("/firm_survey_en.doc");
?>

Le résultat est le suivant :
- LE téléchargement se réalise, mais le problème, das les prop´çiéte du fichier Word téléchargé il est de 1 k, alors que sa taille réelle est de 34 kbyte. Dans le résultat c'est un fichier word sans contenu.


Si vous avez une piste d'ou provient mon problème merci de bien vouloir me l'indiquer

-
A voir également:

2 réponses

Pierre Crevoisier
 
Voici un script qui fonctionne!

<?php

# download.php
# scope: force a file to be downloaded and not executed by the
# server
# arguments: $filename

/* set the mime type file; check your Apache server configuration:
*/
$MIME_TYPES_FILE = "/conf/mime.types";
/* set here the path to your files (dont forget trailing slash at the end /)
*/
$FILESERVER = "/temp/myfiles/";


/* build an array keyed on the file ext */
if (is_readable($MIME_TYPES_FILE)) {
$mime_types=array();
/* open our mime.types file for reading */
$mtype_f=fopen($MIME_TYPES_FILE,"r");
while (!feof($mtype_f)) {
/* pull a line off the file */
$mt_buf=trim(fgets($mtype_f,1024));
/* discard if the line was blank or started with a comment */
if (strlen($mt_buf) > 0) if (substr($mt_buf,0,1) != "#") {
/* make temp array of the mime.types line we just read */
$mt_tmp=preg_split("/[\s]+/", $mt_buf, -1, PREG_SPLIT_NO_EMPTY);
$mt_num=count($mt_tmp);
/* if $mt_num = 1 then we got no file extensions for the type */
if ($mt_num > 1) {
for ($i=1;$i<$mt_num;$i++) {
/* if we find a comment mid-line, stop processing */
if (strstr($mt_tmp[$i],"#")) {
break;
/* otherwise stick the type in an array keyed by extension */
} else {
$mime_types[$mt_tmp[$i]]=$mt_tmp[0];
}
}
/* zero the temporary array */
unset($mt_tmp);
}
}
}
/* close the mime.types file we were reading */
fclose($mtype_f);
} else {
echo "ERROR: unable to read the file " . $MIME_TYPES_FILE . "\n";
}

/* eg, mime_lookup("doc") returns "application/ms-word" */
function mime_lookup($ext) {
global $mime_types;
return $mime_types[$ext];
}

$mimeType = "application/octet-stream"; // default mime type

# set the full path to the file
$fpath = $FILESERVER . $filename ;

if (file_exists($fpath)) {

if ( $filetype = strrchr($filename,".") ) {
$filetype = substr($filetype,1); // Get the file extension ("doc","jpg")
$mimeType = mime_lookup($filetype) ; // Get the mime type
}

header("Content-Disposition: filename=\"$filename\"");
if (!empty($mimeType)) header("Content-Type: $mimeType");
else header("Content-Type: text/plain");
header("Pragma: no-cache");
header("Expires: 0");

header("Content-Length: ". filesize("$fpath"));
$fp=fopen("$fpath","r");
print fread($fp,filesize("$fpath"));
fclose($fp);
} else {
print ("Unable to find your file \"" . $filename . "\"!");
}

?>
1
arfal Messages postés 37 Date d'inscription   Statut Membre Dernière intervention   3
 
alors les gas personne ne s'attribue ??
0