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

时间:2014-09-17 02:05:13

标签: javascript php file-upload html-form

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

我使用的是基本的how to from 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";
        }

是否有其他人遇到此问题或有可能的解决方案?

1 个答案:

答案 0 :(得分:2)

如果用户正在上传文件,则需要执行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>