我正在文件夹和数据库表中插入配置文件图像,图像很好地上传到"uploads"文件夹,但没有插入到mysql数据库表中。我得到这个错误:
Notice: Undefined index: fileToUpload in C:'wamp'www'project'upload_image.php on line 10
来自以下代码:
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 code
<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'];
试试这个。
fileToUpload变量不作为$_POST 存在
$_POST['fileToUpload'];
如果必须像以前使用的一样设置为$_FILE
$_FILE['fileToUpload'];
但是$_FILE["]数组被分解为不同的组件,您需要选择要查找的数组部分。
例如,如果您试图将文件名保存到数据库,请使用
$_FILES["fileToUpload"]["tmp_name"];
以下代码是解决问题的另一种方法:
<?php
$target_dir = "uploads/";
$uploadOk = 1;
if(isset($_POST["submit"])||!empty($_FILES["fileToUpload"]["name"])) {
$target_file =$target_dir.basename($_FILES["fileToUpload"]["name"]);
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
$check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
if($check !== false) {
$check["mime"];
$uploadOk = 1;
if (file_exists($target_file)) {
echo "File already exists.";
$uploadOk = 0;
}
// Check file size
if ($_FILES["fileToUpload"]["size"] > 500000) {
echo "Sorry, your file is too large.";
$uploadOk = 0;
}
} else {
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg") {
echo "Only JPG, JPEG & PNG files are allowed.";
$uploadOk = 0;
}
$uploadOk = 0;
}
if ($uploadOk == 0) {
echo "Sorry, your file was not uploaded.";
} 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.";
}
}}
?>