PHP未定义索引的文件上载问题

File Upload Issue with PHP Undefined Index

本文关键字:上载 问题 文件 未定义 索引 PHP      更新时间:2023-09-26

这个问题已经被问了很多次,尤其是在论坛上,但每个问题都针对每个用户的情况,所以似乎没有任何帮助。

我正在使用w3schools的基本操作方法,但似乎无法实现。。我得到了错误Undefined Index:file,但在我的代码或验证文件中没有任何错误。有人能帮忙吗?

HTML表单:

<form role="form" method="GET" action="<?php echo $processUrl; ?>" enctype="multipart/form-data">
          <div class="form-group col-md-6 col-sm-12">
            <label for="GmailID">Gmail Email Address</label><input type="email" id="GmailID" name="GmailID" class="form-control" placeholder="Example@gmail.com" />
          </div>
          <div class="form-group col-md-6 col-sm-12">
            <label for="GmailPW">Gmail Password</label><input type="password" id="GmailPW" name="GmailPW" class="form-control" placeholder="Gmail Password" />
          </div>
          <div class="form-group col-md-6 col-sm-12">
            <label for="WebmailID">Webmail Email Address</label><input type="email" id="WebmailID" name="WebmailID" class="form-control" placeholder="Example@example.com" />
          </div>
          <div class="form-group col-md-6 col-sm-12">
            <label for="WebmailPW">Webmail Password</label><input type="password" id="WebmailPW" name="WebmailPW" class="form-control" placeholder="Password" />
          </div>
          <div class="form-group col-md-6 col-sm-12">
            <input type="hidden" name="MAX_FILE_SIZE" value="<?php echo $max_file_size ?>" />
            <label for="file">Profile Picture</label><input type="file" id="file" name="file" />
          </div>
          <input type="hidden" id="userID" name="userID" value="<?php echo $_SESSION['user_id']; ?>" />
          <div class="row">
            <button type="submit" class="btn btn-primary pull-right" onclick="return regformhash(this.form,
                                   this.form.GmailID,
                                   this.form.GmailPW,
                                   this.form.WebmailID,
                                   this.form.WebmailPW);">Submit</button>
        </div>
        </form>

PHP脚本:

$allowedExts = array("gif", "jpeg", "jpg", "png");
        $temp = explode(".", $_FILES["file"]["name"]);
        $extension = end($temp);
        if ((($_FILES["file"]["type"] == "image/gif")
        || ($_FILES["file"]["type"] == "image/jpeg")
        || ($_FILES["file"]["type"] == "image/jpg")
        || ($_FILES["file"]["type"] == "image/pjpeg")
        || ($_FILES["file"]["type"] == "image/x-png")
        || ($_FILES["file"]["type"] == "image/png"))
        && ($_FILES["file"]["size"] < 20000)
        && in_array($extension, $allowedExts)) {
          if ($_FILES["file"]["error"] > 0) {
            echo "Return Code: " . $_FILES["file"]["error"] . "<br>";
          } else {
            echo "Upload: " . $_FILES["file"]["name"] . "<br>";
            echo "Type: " . $_FILES["file"]["type"] . "<br>";
            echo "Size: " . ($_FILES["file"]["size"] / 1024) . " kB<br>";
            echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br>";
            if (file_exists("https://example.com/uploads/" . $_FILES["file"]["name"])) {
              echo $_FILES["file"]["name"] . " already exists. ";
            } else {
              move_uploaded_file($_FILES["file"]["tmp_name"],
              "https://example.com/uploads/" . $_FILES["file"]["name"]);
              echo "Stored in: " . "https://example.com/uploads/" . $_FILES["file"]["name"];
            }
          }
        } else {
          echo "Invalid file";
        }

其他人遇到过这种情况吗?或者有可能的解决方案吗?

如果用户正在上传文件,则需要执行POST请求。此外,在提交表单中,您没有将file作为submit操作的一部分,这在返回页面上是未定义的,因为随后会丢失信息。

<button type="submit" class="btn btn-primary pull-right" onclick="return regformhash(
this.form,
this.form.GmailID,
this.form.GmailPW,
this.form.WebmailID,
this.form.WebmailPW,
this.form.file);">Submit</button>