繁体   English   中英

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

[英]File Upload Issue with PHP Undefined Index

这个问题已经问了很多遍了,尤其是在论坛上,但是每个问题都是针对每个用户的情况,因此似乎没有一个问题。

我正在使用从w3schools的基本操作方法,但似乎无法正常工作。.我收到了错误的Undefined Index:文件,但在我的代码或验证文件中看起来没有什么问题。 有人可以帮忙吗?

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动作的一部分包括在内,由于返回信息随后丢失,因此在返回页面上未定义file

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM