if(isset($ _ FILES))不能在PHP中工作

时间:2018-04-24 09:10:17

标签: php forms file

我无法使用$ _FILES,isset($ _ FILES [' NAME'])从html表单中获取输入类型文件值,以从表单获取上传的图像值,i已经发出回声'字符串'在isset条件的第一行并且它没有显示它

 <form role="form" id="contact_form" class="contact-form" method="post" enctype="multipart/form-data">
                                    <div class="row">
                                        <div class="col-md-12">
                                            <ul class="row">
                                                <li class="col-sm-12">
                                                    <label>
                          <input type="text" class="form-control" name="newpseudo" id="name" placeholder="php">
                        </label>
                                                </li>
                                                <li class="col-sm-12">
                                                    <label>
                          <input type="email" class="form-control" name="newmail" id="email" placeholder="*EMAIL">
                        </label>
                                                </li>
                                                <li class="col-sm-12">
                                                    <label>
                          <input type="password" class="form-control" name="newmdp1" id="company" placeholder="*MOT DE PASSE">
                        </label>
                                                </li>
                                                <li class="col-sm-12">
                                                    <label>
                          <input type="password" class="form-control" name="newmdp2" id="company" placeholder="*CONFIRMER VOTRE MOT DE PASSE">
                        </label>
                                                </li>

                                                       <li class="col-sm-12">
                                                        <label>Ajouter Photo de profil</label>
                                                        <input type="file" name="avatar" >
                                                    </li>

                                            </ul>
                                        </div>
                                        <div class="col-md-12">
                                            <ul class="row">
                                                <li class="col-sm-12 no-margin">
                                                    <button type="submit" value="submit" class="btn" id="btn_submit" onClick="proceed();" name="modifier">Mettre a jour</button>
                                                </li>
                                            </ul>
                                        </div>
                                    </div>
                                </form>

PHP代码我试图获取文件输入值:

  if (isset($_FILES['avatar']) AND !empty($_FILES['avatar']['name'])) {
         echo "string";
         $taillemax = 2097152;
         $extensionsvalides = array('jpg','jpeg','gif','png');
         if ($_FILES['avatar']['size'] <= $taillemax) {
             $extensionsUpload = strtolower(substr(strrchr($_FILES['avatar']['name'],'.'),1));
             if (in_array($extensionsUpload, $extensionsvalides)) {
                     $chemin ="membres/avatars/".$_SESSION['id'].".".$extensionsUpload;
                     $resultat = move_uploaded_file($_FILES['avatar']['tmp_name'], $chemin);
                     if ($resultat) {
                         $updateavatar = $bdd->prepare('UPDATE memebre SET avatar = :avatar WHERE id = :id');
                         $updateavatar->execute(array('avatar'=>$_SESSION['id'].".".$extensionsUpload,'id' => $_SESSION['id']));
                               header("Location: userProfile.php?id=".$_SESSION['id']);

                     }
                     else
                     {
                        $msg = "Erreur de l'importation de votre photo de profil";
                     }
             }
             else
             {
                $msg = "votre photo de profil  doit etre de format jpg , jpeg ou png";
             }
         }

1 个答案:

答案 0 :(得分:0)

为表单添加操作并将数据发送到文件执行phpcode并保存文件 这意味着你应该改变这一行:

 <form role="form" id="contact_form" class="contact-form" method="post" 
 enctype="multipart/form-data">

为:

 <form role="form" id="contact_form" class="contact-form" method="post" 
 enctype="multipart/form-data" action="example.php">