Inserer une image php

Fermé
chaimae226 Messages postés 5 Date d'inscription samedi 13 avril 2019 Statut Membre Dernière intervention 14 avril 2019 - Modifié le 13 avril 2019 à 17:20
jordane45 Messages postés 38145 Date d'inscription mercredi 22 octobre 2003 Statut Modérateur Dernière intervention 25 avril 2024 - 14 avril 2019 à 09:22
Bonjour, 

J'essaye d'enregistrer une image mais cela m'affiche un problème.


voici mon code:
<?php
     
if(isset($_POST['enr'])){
     
        $img_blob = addslashes($_FILES['image']['tmp_name']);
        $img_blob = file_get_contents($_FILES['image']['tmp_name']);
        $img_blob = base64_encode($img_blob);
 $x = $bd->prepare("INSERT INTO user(nom,image) VALUES(:nom,:image) WHERE login= :login");
 $x->execute([
      'nom' => $_POST['nom'],
      
      'image' => $img_blob,
   'login' => $_SESSION['login']]);
 
} ?>

2 réponses

jordane45 Messages postés 38145 Date d'inscription mercredi 22 octobre 2003 Statut Modérateur Dernière intervention 25 avril 2024 4 650
13 avril 2019 à 17:14
Bonjour,

Pour commencer, merci d'utiliser la coloration syntaxique pour poster ton code
explications disponibles ici : https://codes-sources.commentcamarche.net/faq/11288-poster-un-extrait-de-code

Ensuite, le message indique que ta variable n'existe pas (pour les undefined index ) et que ta variable $_FILES est vide pour le troisième message....
https://forums.commentcamarche.net/forum/affich-37636387-php-notice-undefined-index

D'où proviennent tes variables ?
Un formulaire ? Quel est son code ?

Je t'invite au passage à appliquer ceci :
https://forums.commentcamarche.net/forum/affich-37584947-php-gestion-des-erreurs-debogage-et-ecriture-du-code
Et ceci :
https://forums.commentcamarche.net/forum/affich-37584941-php-pdo-gerer-les-erreurs
1
chaimae226 Messages postés 5 Date d'inscription samedi 13 avril 2019 Statut Membre Dernière intervention 14 avril 2019
Modifié le 13 avril 2019 à 17:28
<?php session_start(); ?>
<!DOCTYPE html>
<html>
<body>

<form class="form-horizontal"  method="post">
 <input type="text" class="form-control" id="exampleInputName1" name="nom" placeholder="Entrer votre nom" value="<?php echo $d['prof'];?>">
  <input type="file" id="exampleInputFile" name="image" >
                        
                        
 <input type="submit" class="btn btn-primary" name="enr">
  </form>
 
 <?php
     
if(isset($_POST['enr'])){
     
        $img_blob = addslashes($_FILES['image']['tmp_name']);
        $img_blob = file_get_contents($_FILES['image']['tmp_name']);
        $img_blob = base64_encode($img_blob);
 $x = $bd->prepare("INSERT INTO user(nom,image) VALUES(:nom,:image) WHERE login= :login");
 $x->execute([
      'nom' => $_POST['nom'],
      
      'image' => $img_blob,
   'login' => $_SESSION['login']]);
 
} ?>
</body>
</html>
0
jordane45 Messages postés 38145 Date d'inscription mercredi 22 octobre 2003 Statut Modérateur Dernière intervention 25 avril 2024 4 650
13 avril 2019 à 17:55
Il te manque le
enctype="multipart/form-data"

dans ta balise form
0
chaimae226 Messages postés 5 Date d'inscription samedi 13 avril 2019 Statut Membre Dernière intervention 14 avril 2019
Modifié le 13 avril 2019 à 19:20
Il m'affiche toujours cet avertissement: file_get_contents (): le nom de fichier ne peut pas être vide
0
jordane45 Messages postés 38145 Date d'inscription mercredi 22 octobre 2003 Statut Modérateur Dernière intervention 25 avril 2024 4 650 > chaimae226 Messages postés 5 Date d'inscription samedi 13 avril 2019 Statut Membre Dernière intervention 14 avril 2019
14 avril 2019 à 01:27
fais un
print_r($_FILES);

au début de ton code et regarde ce qu'il contient.
Il serait bien également que tu postes ton code modifié (en ayant tenu compte des conseils donnés dans mes liens précédents )
0
chaimae226 Messages postés 5 Date d'inscription samedi 13 avril 2019 Statut Membre Dernière intervention 14 avril 2019
Modifié le 14 avril 2019 à 03:44
voici mon nouveau code:
<?php session_start(); ?>

<!DOCTYPE html>
<html>
<body>

<form class="form-horizontal"  method="post" enctype="multipart/form-data">
 <input type="text" class="form-control" id="exampleInputName1" name="nom" placeholder="Entrer votre nom" value="<?php echo $d['prof'];?>">
  <input type="file" id="exampleInputFile" name="image" >
                        
                        
 <input type="submit" class="btn btn-primary" name="enr">
  </form>
 
 <?php
     
if(isset($_POST['enr'])){
     print_r($_FILES);
        $img_blob = addslashes($_FILES['image']['tmp_name']);
        $img_blob = file_get_contents($_FILES['image']['tmp_name']);
        $img_blob = base64_encode($img_blob);
 $x = $bd->prepare("INSERT INTO user(nom,image) VALUES(:nom,:image) WHERE login= :login");
 $x->execute([
      'nom' => $_POST['nom'],
      
      'image' => $img_blob,
   'login' => $_SESSION['login']]);
 $m = $x->fetch();
} ?>
</body>
</html>

cela ne m'affiche aucune erreur et il n'y a aucun avertissement mais quand je retourne vers ma base de donnée l'image n'a pas été inséré.
0
jordane45 Messages postés 38145 Date d'inscription mercredi 22 octobre 2003 Statut Modérateur Dernière intervention 25 avril 2024 4 650
14 avril 2019 à 09:22
Tu n'as rien appliqué des liens que je t'ai donné ?!!
0