$_SESSION 与 $_FILES 冲突

我有一个表单,它有一个文件类型输入来上传图像,但它总是会显示“通知:未定义的索引”,我看到 $_FILE 变空了,但我在网上找到的答案都没有帮助我.


我有一个项目,用户注册后他必须登录,他的第一页确认他的数据并插入一些新的,其中一个新的是他可以上传的图片,正如我所说,我最初的问题是$_FILES 总是空的,我在网上搜索,这是一个相当常见的错误,但没有一个答案对我有帮助,经过大量尝试,我发现了问题所在,但我仍然不知道为什么,或者如何要解决这个问题。


在我谈到的页面的开头,这里有一段代码:


     if(isset($_SESSION['logado'])){

        $dados =  $_SESSION['dadosUsu'];

     }else{

        unset($_SESSION['dadosUsu']);

        session_destroy();

        header("Location: homeLandingPage.php");

     }

阻止未经授权的人进入页面或使用用户数据创建一个变量,以便使用用户已经注册的信息填写一些输入。


我发现如果我从代码中删除了这一点,$_FILES 全局开始工作并且代码运行良好,但是如果代码在那里问题继续存在,我不知道为什么会发生这种情况,我在网上找不到任何东西谈论与 $_FILES 全局冲突的会话。


之后有一点ajax:


     $(function(){

         $('.form').submit(function(){

             $.ajax({

               url: 'cod_alterarAcc.php',

               type: 'POST',

               data: $('.form').serialize(),

               success: function(data){

                   if(data != ''){

                      $('.recebeDados').html(data);

                      document.getElementById('visor1').value = '<?= $dados['nomeUsu']; ?>';

                      document.getElementById('visor2').value = '<?= $dados['emailUsu']; ?>';

                      document.getElementById('visor3').value = '<?= $dados['emailUsu']; ?>';

                      document.getElementById('visor4').value = '';

                      document.getElementById('visor5').value = '';

                      document.getElementById('visor6').value = '';

                    }

                 }

              });

            return false;

         });

      });

我不知道是不是这个,但这里是在输入类型文件上调用 onChange 的预览函数:


     function previewImagem() {

         var imagem = document.querySelector('input[name=img]').files[0];

         var preview = document.querySelector('img[id=dup]');


         var reader = new FileReader();


         reader.onloadend = function () {

             preview.src = reader.result;

         }


         if (imagem) {

             reader.readAsDataURL(imagem);

         } else {

             preview.src = "";

         }

     }

守候你守候我
浏览 177回答 1
1回答

潇潇雨雨

表单的默认操作似乎与您的 AJAX 冲突,但如果您只想在上传文件时使用 AJAX,那么您希望阻止表单的默认操作。您的 AJAX 请求应如下所示。$('.form').submit(function(e){&nbsp; &nbsp; e.preventDefault();&nbsp; &nbsp; // Preventing the default action of the form&nbsp; &nbsp; var formData = new FormData(this); // So you don't need call serialize()&nbsp; &nbsp; $.ajax({&nbsp; &nbsp; &nbsp; &nbsp; url: 'cod_alterarAcc.php',&nbsp; &nbsp; &nbsp; &nbsp; type: 'POST',&nbsp; &nbsp; &nbsp; &nbsp; data: formData,&nbsp; &nbsp; &nbsp; &nbsp; success: function (data) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(data != ''){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $('.recebeDados').html(data);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; document.getElementById('visor1').value = '<?= $dados['nomeUsu']; ?>';&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; document.getElementById('visor2').value = '<?= $dados['emailUsu']; ?>';&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; document.getElementById('visor3').value = '<?= $dados['emailUsu']; ?>';&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; document.getElementById('visor4').value = '';&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; document.getElementById('visor5').value = '';&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; document.getElementById('visor6').value = '';&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; &nbsp; cache: false,&nbsp; &nbsp; &nbsp; &nbsp; contentType: false,&nbsp; &nbsp; &nbsp; &nbsp; processData: false&nbsp; &nbsp; });});还将您的表单放入 FormData 并指定您的 ajax 请求类型。编辑尝试确认 PHP 是否能够获取数据print_r($_POST);print_r($_FILES);在你的 AJAX success functionconsole.log(data);编辑忘记放参数e了form.submit
打开App,查看更多内容
随时随地看视频慕课网APP