I have a working image upload that saves images to an upload directory. I'm now trying to insert the filepath along with an image name into my mysql database using PDO.
No errors are being thrown but the insertion into the database isn't working. The SQL statement seems to halt the program when its run as any echo after it will not output anything.
This is my input form:
<!DOCTYPE html>
<html>
<body>
<form action="views/imageupload/imageupload.php" method="post" enctype="multipart/form-data">
<label>Image Tag: <input type="text" name="img_tag"></label>
<input type="file" name="fileToUpload" id="fileToUpload">
<input type="submit" value="Upload Image" name="submit">
</form>
</body>
</html>
And this is my php upload:
<?php
$target_dir = "uploads/";
$target_store_url = basename($_FILES["fileToUpload"]["name"]);
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
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;
}
}
$image_insert_tag = $_POST['img_tag'];
$image_insert_SQL = "INSERT INTO images VALUES ('','$image_insert_tag', $target_store_url)";
$insert_exec = $link->query($image_insert_SQL);
// Check if uploadOK = 0 because of an error
if ($uploadOk == 0) {
echo "Sorry, your file was not uploaded.";
//Upload file - code
} 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.";
}
}
?>
For clarification, both of these files are in 'views/imageupload' and inside this directory is another directory called 'uploads' for storing the images. This is all accessed from an index page that includes this page when its clicked on in the nav bar.
I've been reading loads of other questions along these lines but I haven't managed to fix it yet. The reason I'm trying to do this is because I'm going to create another page that will be a gallery and display all the uploaded images. To do this I'll use a statement to pull all the image URL's and display them. I had thought about storing the images themselves in the database but was told that is advised against.