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