Forcer un telechargement

Mammouth du PHP | 620 Messages

21 mai 2010, 23:34

Bonjour,

sur mon site internet j'ai des fichiers pdf
exemple :
  <a href="../cartes/carte-des-Vins.pdf" target="_blank"><img src="../images/carte-vins.jpg" alt="carte des vins" width="135" height="200" /></a> 
je souhaiterai au lieu que lorsque l'internaute clique sur le lien et que le fichier s'ouvre directement faire en sorte que le fichier soit telecharger et pas ouvert automatiquement.
alors j'ai cherché comment forcé un télechargement mais je comprend pas comment mettre ça en place.
Je sais que c'est avec des hearders mais je ne comprend pas comment modifié ma ligne actuelle pour avoir le telechargement

Eléphant du PHP | 110 Messages

21 mai 2010, 23:41

Pas du php sa...

Utilise un location header..

Mammouth du PHP | 620 Messages

22 mai 2010, 10:02

oui merci je sais que sa se fait avec le header mais je ne sais pas comment transformé ma ligne de code actuel en consequence.

actuellement j'essai de faire celà :
dans ma page ou j'ai mes liens pdf je met :
<a href="download-doc.php?file=../cartes/carte-des-Vins.pdf">telecharger </a> 
ensuite j'ai créé un page php download-doc.php
<?php 
$file=$_GET['file']; 

//telechargement 
$taille=filesize("../carte/$file"); 
header("Content-Type: application/force-download; name=\"$file\""); 
header("Content-Transfer-Encoding: binary"); 
header("Content-Length: $taille"); 
header("Content-Disposition: attachment; filename=\"$file\""); 
header("Expires: 0"); 
header("Cache-Control: no-cache, must-revalidate"); 
header("Pragma: no-cache"); 
readfile("../carte/$file"); 
exit(); 
?>

mais sa ne fonctionne pas j'ai un ta d'erreurs du genre :
Warning: filesize() [function.filesize]: stat failed for ../carte/carte-des-Vins.pdf in /var/www/monsite.com/download-doc.php on line 5

Warning: Cannot modify header information - headers already sent by (output started at /var/www/monsite.com/download-doc.php:5) in /var/www/monsite.com/download-doc.php on line 6

Warning: Cannot modify header information - headers already sent by (output started at /var/www/monsite.com/download-doc.php:5) in /var/www/monsite.com/download-doc.php on line 7

Warning: Cannot modify header information - headers already sent by (output started at /var/www/monsite.com/download-doc.php:5) in /var/www/monsite.com/download-doc.php on line 8

Warning: Cannot modify header information - headers already sent by (output started at /var/www/monsite.com/download-doc.php:5) in /var/www/monsite.com/download-doc.php on line 9

Warning: Cannot modify header information - headers already sent by (output started at /var/www/monsite.com/download-doc.php:5) in /var/www/monsite.com/download-doc.php on line 10

Warning: Cannot modify header information - headers already sent by (output started at /var/www/monsite.com/download-doc.php:5) in /var/www/monsite.com/download-doc.php on line 11

Warning: Cannot modify header information - headers already sent by (output started at /var/www/monsite.com/download-doc.php:5) in /var/www/monsite.com/download-doc.php on line 12

Warning: readfile(../carte/carte-des-Vins.pdf) [function.readfile]: failed to open stream: No such file or directory in /var/www/monsite.com/download-doc.php on line 13

Mammouth du PHP | 661 Messages

22 mai 2010, 11:21

salut :

est tu sur de la bonne position, dans ton arborescence, de ton pdf ?
par rapport au fichier en cours, il est bien là ? ../carte/../cartes/carte-des-Vins.pdf

Mammouth du PHP | 620 Messages

22 mai 2010, 13:55

bonjour effectivement c'etait une erreur de chemin. sa fonctionne donc maintenant tres bien pour ma page en francais.
Par contre j'utilise la traduction automatique de google pour traduire les pages et si la page est traduite le fichier pdf ne se telecharge pas il s'ouvre et est traduit par google.

ViPHP
ViPHP | 5462 Messages

24 mai 2010, 16:43

par contre
header("Content-Transfer-Encoding: binary");
header("Content-Length: $taille");
header("Content-Disposition: attachment; filename=\"$file\"");
header("Expires: 0");
header("Cache-Control: no-cache, must-revalidate");
header("Pragma: no-cache");
ca sert a rien, c'est les en-tetes par défaut , elle sont mise automatiquement