Page 1 sur 1

Forcer un telechargement

Posté : 21 mai 2010, 23:34
par fabrice88250
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

Re: Forcer un telechargement

Posté : 21 mai 2010, 23:41
par binboum
Pas du php sa...

Utilise un location header..

Re: Forcer un telechargement

Posté : 22 mai 2010, 10:02
par fabrice88250
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

Re: Forcer un telechargement

Posté : 22 mai 2010, 11:21
par Nours312
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

Re: Forcer un telechargement

Posté : 22 mai 2010, 13:55
par fabrice88250
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.

Re: Forcer un telechargement

Posté : 24 mai 2010, 16:43
par stealth35
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