Working on a website where you can add news, that data gets sent to a database and then converted to json, later used in an app. I want to add a feature where when you are creating news, you can attach an image to the news. How do I display the image with the corresponding post? Not just a random uploaded image but the one you chose when you were creating the news.
My idea: My idea is to use the following W3 php scrip to upload the images:
<?php
$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = strtolower(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;
}
}
but modify it so that when you create a news, it will get the news id from the database, make a new folder in /uploads/ setting the name as the id from the news, upload the images to that folder, and then when I want to display the image/s I can get the id of the news I'm viewing and display the images from that folder that corresponds to the id of the news.
That is the only Idea I can come up with and it doesn't seem the greatest. Is this the best way or is there a better way? Thanks