gpt4 book ai didi

PHP FileToUpload 未定义索引错误

转载 作者:太空宇宙 更新时间:2023-11-04 05:18:31 25 4
gpt4 key购买 nike

我想上传文件到uploads ,但我每次都会收到此错误消息:

Notice: Undefined index: fileToUpload in /path/to/dir/upload.php on line 3

HTML 文件:

<form action="php/upload.php" method="post" enctype="multipart/form-data">
<input type="file" name="fileToUpload" id="fileToUpload">
<input type="submit" value="Upload Image" name="submit">
</form>

PHP 文件:

<?php
$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
// Check if image file is a actual image or fake image
if(isset($_POST["submit"])) {
$check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
if($check !== false) {
echo "File is an image - " . $check["mime"] . ".";
$uploadOk = 1;
} else {
echo "File is not an image.";
$uploadOk = 0;
}
}
?>

最佳答案

首先,欢迎来到 Stack Overflow。

您必须将第 3 行移动到 if 语句中,因为如果没有 post,则不会设置全局 $_FILES 变量。

例如:

<?php
// Check if image file is a actual image or fake image
if(isset($_POST["submit"])) {
$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);

$check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
if($check !== false) {
echo "File is an image - " . $check["mime"] . ".";
$uploadOk = 1;
} else {
echo "File is not an image.";
$uploadOk = 0;
}
}
?>

即使如此,也可能没有发布文件

所以:

if(isset($_POST["submit"], $_FILES["fileToUpload"])) {

关于PHP FileToUpload 未定义索引错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45484445/

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