Probleme pour uploader multiple images

Signaler
Messages postés
498
Date d'inscription
lundi 24 août 2009
Statut
Membre
Dernière intervention
1 mars 2020
-
Bonjour,
je veux insérer plusieurs images en même temps en les sélectionnant.
mon code ne fonctionne pas.

voici mon code html :
      <form action="" method="POST" enctype="multipart/form-data"> 
                                <table id="tableOne" class="table no-border custom_table dataTable no-footer dtr-inline">

                                    <thead>
                                        <tr>                        
                                            <th><input type="checkbox" /></th>
                                            <th>Pièces à télécharger</th>
                                            <th>English</th>
                                        </tr>
                                    </thead>
                                    <tbody>
                                    <?php
         if($nbr > 0):
foreach($resulsMDP as $result):
         ?>
                                        <tr>
                                            <td><input type="checkbox" name='id_piece[]' value="<?php echo  $result['id']; ?>" /></td>
                                            <td><?php echo  $result['piece']; ?></td>
                                            <td><input type="file" name="userfile[]" multiple></td>
                                        </tr>
                                         <?php
         endforeach; endif;
         ?>
                                        <tr>
                                          <td> </td>
                                          <td colspan="2"><hr class="mb-4">
  <div class="row">
    <div class="col-md-3"></div>
    <div class="col-md-6">
      <input type="submit" class="btn btn-primary btn-lg btn-block" name="submit" value="Enregistrer" />
      </div>
  <div class="col-md-3"></div>
</div></td>
                                        </tr>
                                    </tbody>
                                </table>
                                </form>


voici mon code php :
if(isset($_POST['submit'])){ 
$msg = 'CODE TEST : '.$_GET['code'].' pseudo '.$_SESSION['pseudo']; 
     echo '<script>alert(\' '.$msg.' \');</script>';
  
  $uploaddir = 'uploads/';
  
  $id_piece      = $_POST['id_piece'];
  //foreach ($_FILES['userfile']['error'] as $key => $error):
     foreach($id_piece as $key=> $hobbys):
//images

            $tmp_name = $_FILES['userfile']['tmp_name'][$key];
            $name = $_FILES['userfile']['name'][$key];
            $uploadfile = $uploaddir . basename($name);
 
            if (move_uploaded_file($tmp_name, $uploadfile))
            {
                echo "Success: File ".$name." uploaded.<br/>";
            }
            else
            {
                echo "Error: File ".$name." cannot be uploaded.<br/>";
            }
        

// INSERTION 
$tab2 = array(':id_piece' => $id_piece[$key]
    ,':img'      => $uploadfile
    ,':code'     => $_GET['code']
    ,':session'  =>  $_SESSION['pseudo']
             );
//création de la requête SQL:
       $sql2 = "INSERT INTO img_pieces(id_piece
                                   ,img
           ,code
           ,session
           ) 
                          VALUES (:id_piece
                ,:img
          ,:code
          ,:session
         )" 
;
$req2 = $pdo->prepare($sql2);
$result = $req2->execute($tab2);
endforeach;
} 
//VERIF L'EXISTENCE DU TELEPHONE
  $sql_1 = "SELECT * 
     FROM pieces_a_fournirs
     ORDER BY piece
          ";

// on envoie la requête
try{
     $req_1 = $pdo->prepare($sql_1);
     $req_1->execute();
     $resulsMDP = $req_1->fetchAll();
}catch(PDOException  $e){
     echo "ERREUR DE REQUETE : " . $sql_1 . '  error : '.$e->getMessage();
}

//echo $sql;
$nbr = !empty($resulsMDP) ? count($resulsMDP) : 0;


voici la structure de la table :
CREATE TABLE IF NOT EXISTS `img_pieces` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `id_piece` int(11) NOT NULL,
  `img` varchar(225) NOT NULL,
  `code` int(60) NOT NULL,
  `session` varchar(225) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=4 ;