8791 sujets

Développement web côté serveur, CMS

Bonjour à tous!
Je souhaite forcer le téléchargement de certains fichiers. Voici donc le code utilisé :

<?php
if( $_POST && $_POST['document'] )
{
	$document = htmlentities( $_POST['document'] , ENT_QUOTES , "UTF-8" );
	$pieces = explode('/',$document);
	$nomFichier = $pieces[count($pieces)-1];
	$document = utf8_decode($document);
	//$document = utf8_decode("contenu/Qualité/hé hé.doc");
	
	header('Content-Type: text/html; Charset=UTF-8');
	header('Content-disposition: attachment; filename='.$nomFichier);
	header('Content-Type: application/force-download');
	header('Content-Transfer-Encoding: fichier'); 
	header('Content-Length: '.filesize($document));
	header('Pragma: no-cache');
	header('Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0');
	header('Expires: 0');
	readfile("$document");
}
?>


Cependant, cela ne marche pas. La fenêtre s'ouvre, mais il semble que le chemin d'accès ne soit pas bon (pourtant il est bon, j'ai vérifié plusieurs fois). Il m'est donc venu à l'esprit qu'il n'aimais pas l'utf-8.
Le plus bizarre étant que la ligne de test (mise en commentaire) dans ce bout de code fonctionne parfaitement.
Et quand j'affiche le chemin (celui qui ne fonctionne pas), il s'affiche parfaitement.
Le fichier (erroné) que je récupère est rempli par les erreurs rencontrées :

<br />
<b>Warning</b>:  filesize() [<a href='function.filesize'>function.filesize</a>]: stat failed for contenu/Qualit&amp;eacute;/1-MQ-Managt Qualit&amp;eacute;/Recueil qualit&amp;eacute;/Note-SInformation-240306.doc in <b>G:\Stage\intranet_point\download.php</b> on line <b>14</b><br />
<br />
<b>Warning</b>:  Cannot modify header information - headers already sent by (output started at G:\Stage\intranet_point\download.php:14) in <b>G:\Stage\intranet_point\download.php</b> on line <b>14</b><br />
<br />
<b>Warning</b>:  Cannot modify header information - headers already sent by (output started at G:\Stage\intranet_point\download.php:14) in <b>G:\Stage\intranet_point\download.php</b> on line <b>15</b><br />
<br />
<b>Warning</b>:  Cannot modify header information - headers already sent by (output started at G:\Stage\intranet_point\download.php:14) in <b>G:\Stage\intranet_point\download.php</b> on line <b>16</b><br />
<br />
<b>Warning</b>:  Cannot modify header information - headers already sent by (output started at G:\Stage\intranet_point\download.php:14) in <b>G:\Stage\intranet_point\download.php</b> on line <b>17</b><br />
<br />
<b>Warning</b>:  readfile(contenu/Qualit&amp;eacute;/1-MQ-Managt Qualit&amp;eacute;/Recueil qualit&amp;eacute;/Note-SInformation-240306.doc) [<a href='function.readfile'>function.readfile</a>]: failed to open stream: No such file or directory in <b>G:\Stage\intranet_point\download.php</b> on line <b>18</b><br />


Et là on peut voir qu'il y a une erreur d'encodage (enfin je pense).
Quelqu'un aurit-il une idée de comment résoudre ce problème?

Merci d'avance
Modifié par arnaultp (04 Sep 2007 - 00:25)
Bonjour,

Les warning suivant c'est parce que tu as une erreur qui s'affiche avant tes header donc ils ne peuvent pas fonctionner, il faudrai que tu verifies ta ligne 14 ou il y a le filesize, C'est sans doute cette ligne qui entraine tout le bazar.

Car pour que les header fonctionne tu ne dois rien afficher sur ta page avant. donc aucun texte, et s'il y a une erreur cela ne fonctionne plus.
C'est un pote qui a trouvé la solution.
Voici le code corrigé:


<?php
if( $_POST && $_POST['document'] )
{
	$document = html_entity_decode( $_POST['document'] , ENT_QUOTES , "UTF-8" );
	$pieces = explode('/',$document);
	$nomFichier = str_replace( "\\'" , "'" , $pieces[count($pieces)-1] );
	$document = str_replace( "\\'" , "'" , utf8_decode($document) );
	//$document = utf8_decode("contenu/Qualité/1-MQ-Managt Qualité/Recueil qualité/Notepersonnel-140906.doc");
	
	header('Content-Type: text/html; Charset=UTF-8');
	header('Content-disposition: attachment; filename='.utf8_decode($nomFichier));
	header('Content-Type: application/force-download');
	header('Content-Transfer-Encoding: fichier'); 
	header('Content-Length: '.filesize($document));
	header('Pragma: no-cache');
	header('Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0');
	header('Expires: 0');
	readfile("$document");
}
?>


Apparemment htmlentities était inefficace... Et un autre problème intervient par la suite. C'est que les apostrophe ( ' ) sont remplacés par des ( \' ). Il font donc les changer dans la chaîne.
Merci de t'être penché sur le problème en tous cas Halindel.