Problème de upload de fichier

Petit nouveau ! | 8 Messages

27 avr. 2017, 12:26

Bonjour,

Comme vous vous en doutez je poste ce message car j'ai un problème, en effet je tente de créer une page qui permettra l'upload d'un fichier, j'ai donc repris les codes d'un autre site dans lequel cela fonctionne parfaitement et j'ai effectué les modifications nécessaires (nom des variables, adresse de dossiers,...), mais ça ne fonctionne pas.

Cela quelques semaines que je me rechercher, que je relis mon code, mais rien n'y fais, j'ai testé l'insertion des infos sans le fichier à uploader cela fonctionne bien, donc le problème vient du code d'upload mais je ne trouve pas l'erreur.

Je vous fais parvenir les 2 codes, celui du formulaire "ajout_ad.php" et celui du traitement "ajout_ad2.php"

ajout_ad.php :

Code : Tout sélectionner

<?php include("include/bdd.inc.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>AJOUT - ADHERENTS</title> <link rel="shortcut icon" type="image/x-icon" href="images/favicon.ico" /> <!--<link rel="icon" type="image/jpg" href="images/favicon.jpg">--> <link href="css/reset.css" rel="stylesheet" type="text/css" /> <link href="css/menu.css" rel="stylesheet" type="text/css" /> <link href="css/design.css" rel="stylesheet" type="text/css" /> </head> <body> <div id="contains"> <?php include("include/inc_header.php"); ?> <?php include("include/inc_menu.php"); ?> <div id="page" class="bor_general"> <div id="corps"> <div id="tit_corps"> <h2 class="gen">AJOUT D'ADHERENTS</h2> <div class="clear"></div> </div> <form method="post" action="ajout_ad2.php" id="form" enctype="multipart/form-data" > <table> <tr> <td width="120" class="tit_champ">NOM :</td> <td width="600"><input name="ad_nom" type="text" id="nom" class="txt_champ" /></td> </tr> <tr> <td width="119" class="tit_champ">ADRESSE :</td> <td width="423"><input name="ad_adresse" type="text" id="adresse" class="txt_champ" /></td> </tr> <tr> <td width="119" class="tit_champ">SITE :</td> <td width="423"><input name="ad_site" type="text" id="site" class="txt_champ" /></td> </tr> <tr> <td width="119" class="tit_champ">LOGO* :</td> <td class="champfichier"><input type="hidden" name="MAX_FILE_SIZE" value="1000000" /><input name="avatar" type="file" /></td> </tr> <tr> <td width="119" class="tit_champ">ACTIF</td> <td class="champfichier"><input name="actif" type="checkbox" id="actif" /></td> </tr> <tr> <td>&nbsp;</td> <td><input name="envoyer" type="image" src="design/envoyer.png" value="envoyer" /></td> </tr> </table> </form> <p>* fichier ".png" - dimension : 200x150</p> <div class="clear"></div> </div> <div id="bandeau"> <?php include("include/inc_bandeau_droit.php"); ?> </div> </div> <div class="clear"></div> </div> <?php include("include/inc_footer.php"); ?> </div> <!--<div id="coprs"> <div id="tit_corps"> <h2 class="gen">LISTE DES ADHERENTS</h2> <table width="680" border="0 "> <tr> <td width="30" class="tit_tab">N°</td> <td width="150" class="tit_tab">NOM</td> <td width="200" class="tit_tab">ADRESSE</td> <td width="100" class="tit_tab">SITE</td> <td width="120" class="tit_tab">LOGO</td> <td width="50" class="tit_tab">Actif</td> <td width="30" class="tit_tab">Sup.</td> </tr> <?php $reponse = $bdd->query('SELECT * FROM adherents ORDER BY ad_nom'); while ($donnees = $reponse->fetch()) { ?> <tr> <td class="result_tab"><?php echo htmlspecialchars($donnees['id']); ?></td> <td class="result_tab"><?php echo htmlspecialchars($donnees['ad_nom']); ?></td> <td class="result_tab"><?php echo htmlspecialchars($donnees['ad_adresse']); ?></td> <td class="result_tab"><?php echo htmlspecialchars($donnees['ad_site']); ?></td> <td class="result_tab"><?php echo htmlspecialchars($donnees['ad_logo']); ?></td> <td class="result_tab"><?php echo htmlspecialchars($donnees['actif']); ?></td> <td class="result_tab"><a href="javascript:if(confirm('Etes-vous sûr de vouloir supprimer cet adhérent : <?php echo addslashes($donnees['ad_nom']); ?> ?')){document.location.href='suppr_ad.php?id=<?php echo $donnees['id']; ?>';}">S</a></td> </tr> <?php } $reponse->closeCursor(); ?> </table> <p> Pour ajouter un nouveau "adherent" : <a href="ajout_ad.php">CLIQUEZ ICI</a></p> </div> <div class="clear"></div> </div> <div id="bandeau"> <?php include("include/inc_bandeau_droit.php"); ?> </div> </div> <?php include("include/inc_footer.php"); ?> </div>--> </body> </html>

ajout_ad2.php :

Code : Tout sélectionner

<?php include("include/bdd.inc.php"); ?> <?php function filter($in) { $search = array ('@[éèêëÊË]@i','@[àâäÂÄ]@i','@[îïÎÏ]@i','@[ûùüÛÜ]@i','@[ôöÔÖ]@i','@[ç]@i','@[ ]@','@[\']@'); $replace = array ('e','a','i','u','o','c','_',''); return preg_replace($search, $replace, $in); } $ad_nom=htmlspecialchars($_POST["ad_nom"]); $ad_adresse=htmlspecialchars($_POST["ad_adresse"]); $ad_site=htmlspecialchars($_POST["ad_site"]); if(isset($_POST["actif"])){ echo $actif=1 ; } else { echo $actif=0; } if(isset($_FILES['ad_logo'])) { $dossier = 'images/adherents/'; echo "<br>" .$dossier; $ad_logo = basename($_FILES['ad_logo']['name']);echo "<br>" . $_FILES['ad_logo']['tmp_name']; $taille_maxi = 1000000; $taille = $_FILES['ad_logo']['size']; $extensions = array('.png'); $extension = $_FILES['ad_logo']['type']; //Début des vérifications de sécurité... if(!file_exists($dossier)){ echo "<br> Erreur le dossier $dossier n'existe pas !"; } if(!in_array($extension, $extensions)) //Si l'extension n'est pas dans le tableau { $erreur = '<br> Vous devez uploader un fichier de type png...'; } if($taille>$taille_maxi) { $erreur = '<br> Le fichier est trop gros...'; } if(!isset($erreur)) //S'il n'y a pas d'erreur, on upload { //On formate le nom du fichier ici... $ad_logo = strtr($ad_logo, 'ÀÁÂÃÄÅÇÈÉÊËÌÍÎÏÒÓÔÕÖÙÚÛÜÝàáâãäåçèéêëìíîïðòóôõöùúûüýÿ', 'AAAAAACEEEEIIIIOOOOOUUUUYaaaaaaceeeeiiiioooooouuuuyy'); $ad_logo = preg_replace('/([^.a-z0-9]+)/i', '-', $ad_logo); if(move_uploaded_file($_FILES['ad_logo']['tmp_name'], $dossier . $ad_logo)) //Si la fonction renvoie TRUE, c'est que ça a fonctionné... { echo '<br> Upload a IMAGE effectué avec succès !'; } else //Sinon (la fonction renvoie FALSE). { echo '<br> Echec de l\'upload IMAGE !';echo "<br> chemin de destination =>" . $dossier . $ad_logo; } } else { echo $erreur; } $req = $bdd->prepare('INSERT INTO adherents(ad_nom, ad_adresse, ad_site, ad_logo, actif) VALUES (:ad_nom, :ad_adresse, :ad_site, :ad_logo, :actif)'); $req->execute(array( 'ad_nom' => $ad_nom, 'ad_adresse' => $ad_adresse, 'ad_site' => $ad_site, 'ad_logo' => $ad_logo, 'actif' => $actif )); //header("location:liste_adherents.php"); } ?>
D'avance merci de vos retours,
Laetitia
Modifié en dernier par moogli le 27 avr. 2017, 14:34, modifié 1 fois.
Raison : suppression js de stat de visite pour éviter la reprise d'info

Avatar du membre
Modérateur PHPfrance
Modérateur PHPfrance | 8758 Messages

27 avr. 2017, 14:38

salut,

c'est dommage tu y est presque, le champs html de type file s'appel avatar et tu chercher un champs qui s'appel ad_logo dans $_FILES.

Un déboguer (via un IDE) t'aurais aider à résoudre le problème en moins de 2 minutes.
Au pire utiliser les méthodes de débogage empirique avec var_dump ou print_r aurais fait la même chose.
dans ce genre commence toujours pas vérifier le contenu des variables, principalement celle qui viennent des utilisateurs.

ps : la mise en page avec les tables c'est moyen :-)

@+
Il en faut peu pour être heureux ......

Petit nouveau ! | 8 Messages

27 avr. 2017, 15:45

Bonjour,

Tout d'abord, merci de ton temps pour me répondre,

En ce qui concerne le nom du champs, effectivement c'est lors de mes nombreux tests et j'ai oublié de re-modifier ensuite, quoi qu'il en soit ça ne marche pas plus,.

voici le bon code :

Code : Tout sélectionner

<?php include("include/bdd.inc.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>AJOUT - ADHERENTS</title> <link rel="shortcut icon" type="image/x-icon" href="images/favicon.ico" /> <!--<link rel="icon" type="image/jpg" href="images/favicon.jpg">--> <link href="css/reset.css" rel="stylesheet" type="text/css" /> <link href="css/menu.css" rel="stylesheet" type="text/css" /> <link href="css/design.css" rel="stylesheet" type="text/css" /> </head> <body> <div id="contains"> <?php include("include/inc_header.php"); ?> <?php include("include/inc_menu.php"); ?> <div id="page" class="bor_general"> <div id="corps"> <div id="tit_corps"> <h2 class="gen">AJOUT D'ADHERENTS</h2> <div class="clear"></div> </div> <form method="post" action="ajout_ad2.php" id="form" enctype="multipart/form-data" > <table> <tr> <td width="120" class="tit_champ">NOM :</td> <td width="600"><input name="ad_nom" type="text" id="nom" class="txt_champ" /></td> </tr> <tr> <td width="119" class="tit_champ">ADRESSE :</td> <td width="423"><input name="ad_adresse" type="text" id="adresse" class="txt_champ" /></td> </tr> <tr> <td width="119" class="tit_champ">SITE :</td> <td width="423"><input name="ad_site" type="text" id="site" class="txt_champ" /></td> </tr> <tr> <td width="119" class="tit_champ">LOGO* :</td> <td class="champfichier"><input type="hidden" name="MAX_FILE_SIZE" value="1000000" /><input name="ad_logo" type="file" /></td> </tr> <tr> <td width="119" class="tit_champ">ACTIF</td> <td class="champfichier"><input name="actif" type="checkbox" id="actif" /></td> </tr> <tr> <td>&nbsp;</td> <td><input name="envoyer" type="image" src="design/envoyer.png" value="envoyer" /></td> </tr> </table> </form> <p>* fichier ".png" - dimension : 200x150</p> <div class="clear"></div> </div> <div id="bandeau"> <?php include("include/inc_bandeau_droit.php"); ?> </div> </div> <div class="clear"></div> </div> <?php include("include/inc_footer.php"); ?> </div>

Pour le var_dump ou le print_r je ne sais pas comment ça fonctionne ni comment les placer ?

et enfin la mise en page avec les tables, je suis d'accord, je comptais changer cela une fois que le upload fonctionnerait, en tout cas merci du conseil.

Si tu peux m'aider je prend ?

Laetitia

Avatar du membre
Modérateur PHPfrance
Modérateur PHPfrance | 8758 Messages

28 avr. 2017, 08:48

pour var_dump
<?php
var_dump($_POST);
en haut du fichier ajout_ad2.php

@+
Il en faut peu pour être heureux ......

Petit nouveau ! | 8 Messages

28 avr. 2017, 09:25

Bonjour,

Alors je viens de placer le var_dump et voici le retour que j'ai :

array (size=7)
'ad_nom' => string 'CO-DEP - basket' (length=15)
'ad_adresse' => string '25 rue de paris' (length=15)
'ad_site' => string 'www.basket.fr' (length=13)
'MAX_FILE_SIZE' => string '1000000' (length=7)
'actif' => string 'on' (length=2)
'envoyer_x' => string '75' (length=2)
'envoyer_y' => string '14' (length=2)
1

apparemment "ad_logo" n'a rien renvoyé

que faire ?

Merci de ton aide

Avatar du membre
Modérateur PHPfrance
Modérateur PHPfrance | 8758 Messages

28 avr. 2017, 09:36

j'ai oublié pour FILES :s
var_dump($_POST, $_FILES);

Il doit y avoir un index correspondant a ton champ de type file.

ensuite tu ajoutes des var_dump un peu partout pour suivre le code.

@+
Il en faut peu pour être heureux ......

Petit nouveau ! | 8 Messages

28 avr. 2017, 10:56

Voila le message que j'ai reçu :

array (size=7)
'ad_nom' => string 'CO-DEP - basket' (length=15)
'ad_adresse' => string '25 rue de paris' (length=15)
'ad_site' => string 'www.basket.fr' (length=13)
'MAX_FILE_SIZE' => string '1000000' (length=7)
'actif' => string 'on' (length=2)
'envoyer_x' => string '52' (length=2)
'envoyer_y' => string '26' (length=2)

array (size=0)
empty

1

Avatar du membre
Modérateur PHPfrance
Modérateur PHPfrance | 8758 Messages

28 avr. 2017, 13:24

donc le premier c'est le contenu de $_POST, le tableau vide le contenu de $_FILES.

tu as bien sélectionner un fichier avant de cliquer sur le bouton de validation ?
Il en faut peu pour être heureux ......

Petit nouveau ! | 8 Messages

28 avr. 2017, 14:22

oui

Avatar du membre
Modérateur PHPfrance
Modérateur PHPfrance | 8758 Messages

28 avr. 2017, 14:45

donc il y a autre chose que tu ne dit pas ;)

j'ai testé ton code, en supprimant l'action du formulaire pour être au final sur la même page.
Et j'ai ceci en résultat
array (size=7)
'ad_nom' => string 'gfhj' (length=4)
'ad_adresse' => string 'jdhf' (length=4)
'ad_site' => string 'kjdhf' (length=5)
'MAX_FILE_SIZE' => string '1000000' (length=7)
'envoyer_x' => string '21' (length=2)
'envoyer_y' => string '7' (length=1)
'envoyer' => string 'envoyer' (length=7)
array (size=1)
'ad_logo' =>
array (size=5)
'name' => string 'fichier.pdf' (length=11)
'type' => string '' (length=0)
'tmp_name' => string '' (length=0)
'error' => int 2
'size' => int 0

ton code, ainsi est fonctionnel.


@+
Il en faut peu pour être heureux ......

Petit nouveau ! | 8 Messages

28 avr. 2017, 15:55

Donc je dois faire quoi ? pour que ça marche maintenant ? Parce que la c'est du chinois pour moi

Petit nouveau ! | 8 Messages

10 mai 2017, 08:55

Bonjour,

Je suis désolée mais je n'ai pas eu de retour à mon dernier post, apparemment vous avez une solution à mon problème, pouvez-vous me la transmettre ?

D'avance merci,