首页 > 解决方案 > 表单不上传文件

问题描述

我在上传 img 时遇到问题,php 大喊 $_FILES 为空或更具体:

Notice: Undefined index: newAvatar [...]
Notice: Trying to access array offset on value of type null [...]

这是我的表格:

<form action="updateAvatar.php" method="post" id="updateAvatarForm" enctype=”multipart/form-data”&gt;
  <div class="changeAvatar">
     <div class="avatar">
       <img src=<?php echo ($_SESSION['profile'] != NULL) ?  base64_encode($_SESSION['profile']) : "../static/img/blankProfilePic.svg";?> alt="" id="avatarPreview">
       <input type="file" name="newAvatar" id="newAvatar">
       <img src="../static/img/changeProfilePic.svg" alt="" id="changeAvatar">
     </div>
   </div>
 </form>

和简单的 php 图像打印机:

<?php
  echo '<img src='.$_FILES["newAvatar"]["name"].'>';
?>

表单由 js 中的 eventListener 提交:

    newAvatar.addEventListener("change", ()=>{
            updateAvatarForm.submit()
    });

我试图在表单中添加一些诸如“ajax-something”之类的道具,在js中设置超时因为我认为提交可能触发得太快但我被卡住了,我尝试过的一切都不起作用......

如果有人可以提供帮助,我将不胜感激,对不起我的英语不是我的母语。

标签: phpformsblob

解决方案


推荐阅读