Bonjour de nouveau,

Voici un nouveau problème.
Dans ma section messagerie privée, je propose le téléchargement de pièces jointes.
Dans l'idéal, je voudrai placer un bouton plutôt qu'un lien. Sauf que le bouton corrompt mes fichiers.

Voici le code passant par $_GET:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
 
//lien de téléchargement
echo '<a href="pj.php?pj='.$user->piece_jointe.'&token_lect_mess='.$token_lect_mess.'">telechargement</a>';
 
// page de téléchargement -> pj.php
session_start();
if(isset($_GET['pj']) AND isset($_GET['token_lect_mess'])){
	if($_SESSION['token_lect_mess'] == $_GET['token_lect_mess']) {
 
$fichier = "pieces_jointes/".$_GET['pj'];
 
switch(strrchr(basename($fichier), '.')) {
 
case '.doc': $type = 'application/msword'; break;
case '.docx': $type = 'application/vnd.openxmlformats-officedocument.wordprocessingml.document'; break;
case '.xls': $type = 'application/vnd.ms-excel'; break;
case '.xlsx': $type = 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'; break;
case '.ppt': $type = 'application/vnd.ms-powerpoint'; break;
case '.pptx': $type = 'application/vnd.openxmlformats-officedocument.presentationml.presentation'; break;
case '.pdf': $type = 'application/pdf'; break;
case '.png': $type = 'image/png'; break;
case '.jpg': $type = 'image/jpeg'; break;
case '.txt': $type = 'text/plain'; break;
default: $type = 'application/octet-stream'; break;
 
}
 
header('Content-disposition: attachment; filename='.$fichier);
header('Content-Type: application/force-download');
header('Content-Transfer-Encoding: $type\n');
header('Content-Length: '.filesize($fichier));
header('Pragma: no-cache');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0, public');
header('Expires: 0');
readfile($fichier);
}
else {
		header ("Refresh: 1;URL=index.php");
		echo "<div class='apres_valid'>Token erroné.</div>";
		}
}else {
	header("Location:index.php");
}
Ce code fonctionne très bien, par contre, si je tape le nom d'une autre pièce jointe, alors je peux la télécharger... et ceci est fâcheux.


Donc passant par $_POST:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
 
//bouton de téléchargement
echo '
<span class="contenair-mess-lu-repondre">
    <FORM method="POST" action="piece_jointe.php"> 
	    <input type="hidden" name="piece_jointe" value="'.$user->piece_jointe.'" /> 
	    <input type="image" src="'.$urlImg.'piece_jointe.png">
    </FORM>
</span>';
 
//page de téléchargement
$fichier = "pieces_jointes/".$_POST['piece_jointe'];
 
switch(strrchr(basename($fichier), '.')) {
 
case '.doc': $type = 'application/msword'; break;
case '.docx': $type = 'application/vnd.openxmlformats-officedocument.wordprocessingml.document'; break;
case '.xls': $type = 'application/vnd.ms-excel'; break;
case '.xlsx': $type = 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'; break;
case '.ppt': $type = 'application/vnd.ms-powerpoint'; break;
case '.pptx': $type = 'application/vnd.openxmlformats-officedocument.presentationml.presentation'; break;
case '.pdf': $type = 'application/pdf'; break;
case '.png': $type = 'image/png'; break;
case '.jpg': $type = 'image/jpeg'; break;
case '.txt': $type = 'text/plain'; break;
default: $type = 'application/octet-stream'; break;
 
}
 
header('Content-disposition: attachment; filename='.$fichier);
header('Content-Type: application/force-download');
header('Content-Transfer-Encoding: $type\n');
header('Content-Length: '.filesize($fichier));
header('Pragma: no-cache');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0, public');
header('Expires: 0');
readfile($fichier);
Mais la, les téléchargements de pdf et txt fonctionnent bien, mais les fichier de type doc, png, jpg etc ne fonctionnent pas

Quelqu'un a une idée svp?