I'm currently trying to make a gallery with categories and use phpMyadmin andMySQL.
I have been able to upload images on the logged in page and then show it in my gallery (which is on a different page which you do not need to be logged in to see). However, when I tried to add captions to the images I'm uploading as well ad as categories, I'm stuck. This is my code in loggedin.php where the user can upload new pictures.
<section id="uploadsection">
<p id="loggedintext"> Going to upload something? </p>
<form action="templates/upload.php" method="POST" enctype="multipart/form-data" id="uploadform">
<p id="selectimg"> Select image to upload: </p>
<input type="file" name="fileToUpload" id="fileToUpload">
<!-- <input type="text" name="category"> -->
<input id="imgCaption" type="text" name="title" placeholder="Caption">
<input id="subButton" type="submit" value="Upload Image" name="submit">
</form>
</section>
The outcommented part is the one I want to use to sort the gallery into caterories.
This is my upload.php The uploading works, I'm unble to create variables for the category and caption properly.
<?php
// require_once '../../../conf/usjf/accessDB.php';
$target_dir = "../uploads/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
# info
$category = NULL;
$title = NULL;
$image = $_FILES["fileToUpload"]["tmp_name"];
$title = $_GET ["title"] ["file_name"] ;
$filename = $GET{"bilde_filename"};
if(isset($_POST["submit"])) {
if(!empty($_POST["title"])) {
$title = $_POST["title"];
}
if(!empty($_POST["category"])) {
$category = $_POST["category"];
}
// Check if image file is a actual image or fake image
$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"] > 1000000) {
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 {
$sqlString = "INSERT INTO bilde (bilde_filename, bilde_caption)
VALUES ('" . $filename ."', '" . $title ."',)";
mysqli_query($dbLink, $sqlString) or die("could not do everything" . mysqli_error($dbLink));
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.";
}
}
?>
gallery.php
$dirname = "uploads/";
$images = glob($dirname."*.jpg");
foreach($images as $image) {
echo '<img src="'.$image.'" class="bilder">';
}
I'm not sure the coding properly connects with my database as while I'm able to upload images (before I tried to add caption and category), the images aren't shown within my PHPmyAdmin.
I have a table for images called bilde (image) with 3 pars, bilde_id, bilde_filename and bilde_caption. bilde_id is AI.
My kategori table has kat_id and kat_name (they are in the database).
my third table is bilde_kategoi with bilde_id and kat_id.
What I need help with is properly add caption to my images when I upload them as well as category (the best would be the posibility of adding more than one kategory)and then being able to display the caption under my images.