gpt4 book ai didi

php - 错误 : Undefined index - PHP,

转载 作者:行者123 更新时间:2023-11-29 02:22:25 25 4
gpt4 key购买 nike

我在文件夹和数据库表中插入个人资料图像,图像很好地上传到“上传”文件夹,但没有插入到 mysql 数据库表中。我收到此错误:

注意:第 10 行 C:\wamp\www\project\upload_image.php 中未定义索引:fileToUpload

来自以下代码:

upload_image.php

<?php
include_once('dbconn.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"])) {
$user_id = $_POST['user_id'];
$fileToUpload = $_POST['fileToUpload'];
$sql="INSERT INTO profile_image (user_id,fileToUpload) VALUES ('$user_id', '$fileToUpload')";
$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;
}
}

// Check if file already exists
if (file_exists($target_file)) {
echo "Sorry, file already exists.";
$uploadOk = 0;
}

// Check file size
if ($_FILES["fileToUpload"]["size"] > 500000) {
echo "Sorry, your file is too large.";
$uploadOk = 0;
}

// Allow certain file formats
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg" && $imageFileType != "gif" ) {
echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
$uploadOk = 0;
}

// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
echo "Sorry, your file was not uploaded.";
// if everything is ok, try to upload file
}
else {
if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";
}
else {
echo "Sorry, there was an error uploading your file.";
}
}
?>

HTML代码

<form method="post" action="upload_image.php" enctype="multipart/form-data">
<?php if(isset($_SESSION['email'])){?>
<input type="hidden" name="user_id" value="<?php echo ($_SESSION['id']);}else{header("Location: login.php");}?>">
<input type="file" name="fileToUpload">
<input type="submit" value="Upload" name="submit">
</form>

有人知道如何解决这个问题吗?

最佳答案

$fileToUpload = $_FILES['fileToUpload']['name'];

试试这个。

关于php - 错误 : Undefined index - PHP,,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29553962/

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