提问者:小点点

如果(isset($_files))在PHP中不起作用


我无法从使用$_files的html表单中获取输入类型文件值,isset($_files['name'])要从表单中获取上载的图像值,我在isset条件的第一行中放了一个回显“string”,但它没有显示它

 <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个答案

匿名用户

为表单添加动作并将数据发送到文件,执行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">