gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 17:17:13 24 4
gpt4 key购买 nike

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

我正在使用基本的 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";
}

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

最佳答案

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

关于javascript - PHP 未定义索引的文件上传问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25881155/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com