So I need to upload images and their URLs to a mySQL database. Right now I have it working so that an image and its relative path is being uploaded to the database and I am outputting it to JSON like so: (filepath is the relative path)
[{"name":"photo","image":"savedphoto.jpg","filepath":"uploads/savedphoto.jpg"}]
What I want is the full URL like so:
[{"name":"photo","image":"savedphoto.jpg","filepath":"http://mywebsite.com/uploads/savedphoto.jpg"}]
Here is the code I have:
$name = $_POST['new'];
$target_dir = "uploads/";
$uploadOk = 1;
$target_file = $target_dir . basename($_FILES["image"]["name"]);
$image_name = addslashes($_FILES['image']['name']);
// Check if image file is a actual image or fake image
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;
}
$query = mysql_query("INSERT INTO new (name, filepath, image)
VALUES ('$name', '$target_file', '$image_name')");
if($query)
{
echo"Successful";
} else {
echo"Error";
}
}
Is it possible to save the full URL to the database? How would I alter the code? I want to output the data to JSON.
My JSON output file:
if ($result = mysqli_query($con, $sql))
{
$resultArray = array();
$tempArray = array();
while($row = $result->fetch_object())
{
$tempArray = $row;
array_push($resultArray, $tempArray);
}
echo json_encode($resultArray);
}
function_exists('json_encode');