首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在PHP语言中使用$_FILES时,为什么我得到未定义的数组键,并试图访问类型为null的值的数组偏移量

在PHP语言中使用$_FILES时,为什么我得到未定义的数组键,并试图访问类型为null的值的数组偏移量
EN

Stack Overflow用户
提问于 2021-07-16 22:53:40
回答 1查看 426关注 0票数 2

我在我的内容管理系统(内容管理系统)的注册部分这样做。我想添加个人资料图像,我确实喜欢w3schools tuts:https://www.w3schools.com/php/php_file_upload.asp。我在文件中这样做了,但是当我在我的文件中运行时,我得到了一个错误: Warning: Undefined array key "profileimg_regis“in C:\xampp\htdocs\DeknoinarakDev\dashboard\run\code.php on line 99

警告:尝试在第99行的C:\xampp\htdocs\DeknoinarakDev\dashboard\run\code.php中访问null类型的值的数组偏移量

警告:第103行的C:\xampp\htdocs\DeknoinarakDev\dashboard\run\code.php中未定义数组键"profileimg_regis“

警告:尝试在第103行访问C:\xampp\htdocs\DeknoinarakDev\dashboard\run\code.php中null类型的值的数组偏移量

致命错误:未捕获主路径: C:\xampp\htdocs\DeknoinarakDev\dashboard\run\code.php:103堆栈跟踪中的路径不能为空:在第103行的C:\xampp\htdocs\DeknoinarakDev\dashboard\run\code.php中引发#0 C:\xampp\htdocs\DeknoinarakDev\dashboard\run\code.php(103):ValueError(‘’) #1 {main}

这是我的PHP代码:

代码语言:javascript
运行
复制
// Check if image file is a actual image or fake image
if(isset($_POST["register_btn"])) {
  $target_dir = "img/user/";
  $target_file = $target_dir . basename($_FILES["profileimg_regis"]["name"]);
  $uploadOk = 1;
  $imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));

  $check = getimagesize($_FILES["profileimg_regis"]["tmp_name"]);

  if($check !== false) {
    echo "File is an image - " . $check["mime"] . ".";
    $uploadOk = 1;
  }
  else {
    echo "File is not an image.";
    $uploadOk = 0;
  }

  // Check if file already exists
  if (file_exists($target_file)) {
    echo "Sorry, file already exists.";
    $uploadOk = 0;
  }

  // Check file size
  if ($_FILES["profileimg_regis"]["size"] > 500000) {
    echo "Sorry, your file is too large.";
    $uploadOk = 0;
  }

  // Allow certain file formats
  if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg") {
    echo "Sorry, only JPG, JPEG & PNG files are allowed.";
    $uploadOk = 0;
  }

  // Check if $uploadOk is set to 0 by an error
  if ($uploadOk == 0) {
    echo "Sorry, your file was not uploaded.";
  // if everything is ok, try to upload file
  }
  else {
    if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
      echo "The file ". htmlspecialchars( basename( $_FILES["fileToUpload"]["name"])). " has been uploaded.";
    } else {
      echo "Sorry, there was an error uploading your file.";
    }
  }
}

这是我的HTML代码:

代码语言:javascript
运行
复制
<div class="modal-background" id="md-regisadmin">
  <div class="modal-container">
    <div class="modal">
      <div class="modal-header">
        <h2>Admin Account</h2>
        <button onclick="closeModal(document.querySelectorAll('#md-regisadmin'))" type="button" id="dismiss-btn-modal">
          <i class="fa fa-times" onclick="closeModal(document.querySelectorAll('#md-regisadmin'))" id="dismiss-btn-modal"></i>
        </button>
      </div>
      <div class="md-content">
          <div class="form-modal">
            <div class="form-group md">
            <form action="run/code.php" method="post">
              <!--<label for="username_regis">Username:</label>
              <input id="username_regis" type="text" name="username_regis" placeholder="Username" required/>
            </div>
            <div class="form-group md">
              <label for="email_regis">E-mail:</label>
              <input id="email_regis" type="email" name="email_regis" placeholder="Email" required/>
            </div>
            <div class="form-group md">
              <label for="pwd_regis">Password:</label>
              <input id="pwd_regis" type="password" name="pwd_regis" placeholder="Password" required/>
            </div>
            <div class="form-group md">
              <label for="pwdr_regis">Repeat Password:</label>
              <input id="pwdr_regis" type="password" name="pwdr_regis" placeholder="Repeat Password" required/>
            </div>
            <div class="form-group md">-->
              <input id="profileimg_regis" name="profileimg_regis" type="file"/>
            <!--</div>
            <div class="form-group md">
              <label for="utype_regis">Usertype:</label>
              <select id="utype_regis" name="utype_regis"/>
                <option value="noper">No Permission</option>
                <option value="readonly">Reader</option>
                <option value="mod">Moderator (Only Change Data In Website)</option>
                <option value="admin">Administrator</option>
                <option value="superadmin">Super Administrator</option>
                <option value="godper">God Permission</option>
                <option value="root">Root Administrator</option>
              </select>
            </div>
          </div>
        </div>-->
        <div class="modal-footer">
            <button type="submit" name="register_btn">
              Add Admin Account
            </button>
        </form>
        <button type="button" onclick="closeModal(document.querySelectorAll('#md-regisadmin'))" id="dismiss3-btn-modal">
          <a onclick="closeModal(document.querySelectorAll('#md-regisadmin'))" id="dismiss2-btn-modal">Cancel</a>
        </button>
      </div>
    </div>
  </div>
</div>

如果你能帮上忙,谢谢。:)

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-08-03 13:18:09

现在我知道如何修复它了。我把<form>enctype="multipart/form-data"</form>忘在表格顶端了对不起。:)

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68411120

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档