0
我正在构建一个图像上传器,我想通过该图像上传器将图像上传到服务器,并用新的图像替换数据库中的旧图像URL。上传部分工作正常,但我无法在数据库中获取imageURL。有人可以看看我的代码,并告诉我我在哪里做错了吗?将图像上传到服务器并将url保存到服务器
<?php
$target_dir = "media/images/";
$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["uploadImage"])) {
$check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
if ($check != false) {
echo "File is an image - " .$check["mime"]. ".";
$uploadOK = 1;
} else {
echo "File not an image";
$uploadOK = 0;
}
}
// check if file exists
if (file_exists($target_file)) {
echo "sorry File exists";
$uploadOK = 0;
}
// check fle size
if ($_FILES["fileToUpload"]["size"] > 5000000) {
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 and GIF files allowed.";
$uploadOK == 0;
}
// check if $uploadOK is set 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.";
include 'connect.php';
$stmt = $conn->prepare("INSERT INTO image(name,imageURL, image_cat_id, AlbumID) VALUES (home1, ?, 8, 0)");
$stmt->bind_param('s', $target_file);
$stmt->execute();
$stmt->close();
} else {
echo "Sorry, there was an error uploading your file.";
}
}
?>
结果在成功上传:
File is an image - image/png.The file arrowdown_51.png has been uploaded. Fatal error: Call to a member function bind_param() on boolean in C:\xampp\htdocs\WebDevelopment\Mosta Dynamic\CMS\upload.php on line 47
感谢您的回复嗨 - 试图解决方案和错误消失,但是数据库没有更新。 – Chri