insertion d'image dans une base de donnée
- Accueil
- Forum
- Programmation
- PHP
- insertion d'image dans une base de donnée

villa Le 9 juin 2016 à 12:12 (Édité le 25 janvier 2019 à 17:51)
j'ai créer un formulaire et dans mon entré input de type "file" je ne sais pas comment faire pour récupéré la photo un fois sélection fait, vous pouvez m’aider ?????

Ranarxhag Le 9 juin 2016 à 13:36 (Édité le 1 janvier 1970 à 01:00)

Balatharas Le 9 juin 2016 à 17:35 (Édité le 1 janvier 1970 à 01:00)
if(isset($_FILES['avatar']) AND !empty($_FILES['avatar']['name']))
{
$tailleMax = 3145728;
$extensionsValides = array('jpg', 'jpeg', 'gif', 'png', 'tiff');
if($_FILES['avatar']['size'] <= $tailleMax)
{
$extensionsUpload = strtolower(substr(strrchr($_FILES['avatar']['name'], '.'), 1));
if(in_array($extensionsUpload, $extensionsValides))
{
$chemin = "Membres/Avatar/" .$_SESSION['id'].".".$extensionsUpload;
$resultat = move_uploaded_file($_FILES['avatar']['tmp_name'], $chemin);
if($resultat)
{
$updateAvatar = $bdd->prepare('UPDATE membres SET avatar = :avatar WHERE id = :id');
$updateAvatar->execute(array(
'avatar' => $_SESSION['id'].".".$extensionsUpload,
'id' => $_SESSION['id']
));
header('Location: profil.php?id='.$_SESSION['id']);
}
else
{
$erreur = "Erreur inconnue lors de l'importation de l'image.";
}
}
else
{
$erreur = "Votre photo de profil doit être au format: jpg, jpeg, gif, png ou tiff";
}
}
else
{
$erreur = "Votre photo de profil ne doit pas dépasser 2Mo.";
}
}
Et dans ton formulaire voilà ce qu'il faut mettre
<label>Avatar :</label>
<input type="file" name="avatar" />

TheOldNoob Le 10 juin 2016 à 09:34 (Édité le 1 janvier 1970 à 01:00)
Il manque un tout petit truc pour le type MIME
La tu déclare uniquement les sous-type. La bonne formulation serait :
$extentionsValides= array('image/jpeg', 'image/jpg', 'image/png', 'image/gif');
image/ n'étant pas le dossier de réception de l'image, mais bien un type MIME 😀