【问题标题】:File Upload Issue with PHP Undefined IndexPHP未定义索引的文件上传问题
【发布时间】:2014-09-17 02:05:13
【问题描述】:

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

我正在使用基本的how to from w3schools,但似乎无法使其正常工作。我收到错误未定义索引:文件,但在我的代码或验证文件中没有任何问题。有人可以帮忙吗?

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";
        }

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

【问题讨论】:

  • 我相信GET 必须是POST 并且可能检查isset($_FILES['file'])
  • 您没有通过 GET 发送文件。您链接到的教程显示使用 POST。

标签: javascript php file-upload forms


【解决方案1】:

如果用户正在上传文件,您需要发出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>

【讨论】:

    猜你喜欢
    • 2012-04-05
    • 1970-01-01
    • 2014-05-24
    • 1970-01-01
    • 1970-01-01
    • 2014-02-11
    • 1970-01-01
    • 1970-01-01
    • 2014-04-29
    相关资源
    最近更新 更多