Bonjour,
J'appelle à l'aide car je ne parviens pas à trouver la solution pour afficher correctement une image stockée dans une base de données mysql.... Je désespère.
Tout d'abord j'ai créé un fichier index.php :
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "
http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="
http://www.w3.org/1999/xhtml">
<head>
<title>Stock d'images</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Document sans titre</title>
</head>
<body>
<?php
//script de fonction de transfert
include ("transfert.php");
if ( isset($_FILES['fic']) )
{
transfert();
}
?>
<h3>Envoi d'une image</h3>
<form enctype="multipart/form-data" action="#" method="post">
<input type="hidden" name="MAX_FILE_SIZE" value="250000" />
<input type="file" name="fic" size=50 />
<input type="submit" value="Envoyer" />
</form>
<p><a href="liste.php">Liste</a></p>
</body>
</html>
ensuite un fichier me permettant de me connecter à ma base mysql.
puis un fichier pour le transfert : transfert.php
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "
http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="
http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Document sans titre</title>
</head>
<body>
<?php
function transfert ()
{
$ret = false;
$img_blob = '';
$img_taille = 0;
$img_type = '';
$img_nom = '';
$taille_max = 250000;
$ret = is_uploaded_file ($_FILES['fic']['tmp_name']);
if ( !$ret )
{
echo "Problème de transfert";
return false;
}
else
{
// Le fichier a bien été reçu
$img_taille = $_FILES['fic']['size'];
if ( $img_taille > $taille_max )
{
echo "Trop gros !";
return false;
}
$img_type = $_FILES['fic']['type'];
$img_nom = $_FILES['fic']['name'];
include ("connexion.php");
$img_blob = file_get_contents ($_FILES['fic']['tmp_name']);
$req = "INSERT INTO images (".
"img_nom, img_taille, img_type, img_blob ".
") VALUES (".
"'".$img_nom."', ".
"'".$img_taille."', ".
"'".$img_type."', ".
// N'oublions pas d'échapper le contenu binaire
"'".addslashes ($img_blob)."') ";
$ret = mysql_query ($req) or die (mysql_error ());
return true;
} }
?>
</body>
</html>
puis un fichier liste.php pour lister (et vérifier) la présence de mes fichiers images dans la base :
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "
http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="
http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Stock d'images</title>
</head>
<body>
<?php
include ("connexion.php");
$req = "SELECT img_nom, img_id ".
"FROM images ORDER BY img_nom";
$ret = mysql_query ($req) or die (mysql_error ());
while ( $col = mysql_fetch_row ($ret) )
{
echo "<a href=\"apercu.php?id=".$col[1].
"\">".$col[0]."</a><br />";
} ?>
</body>
</html>
et pour finir le fichier aperçu.php pour afficher l'image choisie depuis le fichier liste.php :
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "
http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="
http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Document sans titre</title>
</head>
<body>
<?php
if ( isset($_GET['id']) )
{
$id = intval ($_GET['id']);
include ("connexion.php");
$req = "SELECT img_id, img_type, img_blob ".
"FROM images WHERE img_id = ".$id;
$ret = mysql_query ($req) or die (mysql_error ());
$col = mysql_fetch_row ($ret);
if ( !$col[0] )
{
echo "Id d'image inconnu";
}
else
{
header ("Content-type: ".$col[1]);
echo $col[2];
}
}
else
{
echo "Mauvais id d'image";
} ?>
</body>
</html>
mais voici le résultat ....
Warning: Cannot modify header information - headers already sent by (output started at /homepages/39/d446542339/htdocs/envoi photos/apercu.php:9) in /homepages/39/d446542339/htdocs/envoi photos/apercu.php on line 24
�PNG IHDR[C��yo$iCCPICC Profile8�U�o�T>�oR�? XG��ůUS[���I���J���*$�:7���鶪O{�7�@�H�kk?�<������kkt�q��m�6�nƶ��د�-�mR;`z�����v� x#=\�% �o�Y��Rڱ������#&�?�>�ҹ�Ъ����n�_���;j�;�$}*}+�(}'}/�L�tY�"�$]���.9�⦅%�{�_a݊]h�k�5'SN�{�������<��_������ ����t �jM�{-�4%���Tń�tY۟��R6����#�v\�喊x:��'H���O���3����^�&�����0::�m,L%�3��:qVE� t���]~��I�v�6�Wٯ��) |ʸ2]�G��4��(6w���$��"��A���Ev�m�[D���;�Vh[�}���چ�N|�3��������H��S:����K��t��x��U�'D;7��7;_"��e�?Yqxl+ pHYsg��R!IDATx�[�]Uy�����2�L�B`��",���h���Ւ�]E]>J��B�]5E[5)- h�D P0$�!
Aidez-moi, car je ne trouver aucune réponse à ce problème.
Merci d'avance