当前位置: 首页 > 知识库问答 >
问题:

为什么我有未定义的数组键,并试图访问数组偏移值的类型null错误时使用$_FILES在PHP

岑叶秋
2023-03-14

我在注册部分的内容管理系统中这样做。我想添加配置文件图像,我不喜欢w3学校啧啧:https://www.w3schools.com/php/php_file_upload.asp.我做它ant=其他文件它可以,但当我在我的文件中运行我得到一个错误:警告:未定义的数组键"profileimg_regis"在C:\xampp\htdocs\DeKnoinarakDev\dashboard\在99号线code.php

警告:试图访问C:\xampp\htdocs\deknoarkdev\dashboard\run\code中null类型值的数组偏移量。php在线99

警告:第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堆栈跟踪:#0 C:\xampp\htdocs\DeKnoinarakDev\dashboard\run\code.php(103): getimagesize(")#1{main}抛出在C:\xampp\HTDocs\DeKnoinarakDev\dashboard\run\code.phpon line 103

这是我的PHP代码:

// 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代码:

<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>

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

共有1个答案

向弘懿
2023-03-14

现在我找到了修复它的方法。我忘了

 类似资料: