php - Render Uploaded Images From Mysql To Frontend
Get the solution ↓↓↓I am trying to render uploaded image from Mysql to frontend in a file calledresult.php
This is the code that uploaded the image to Mysql
include ('connect.php');
//if button with the name uploadfilesub has been clicked
if(isset($_POST['uploadfilesub'])) {
//declaring variables
$filename = $_FILES['uploadfile']['name'];
$filetmpname = $_FILES['uploadfile']['tmp_name'];
//folder where images will be uploaded
$folder = 'imagesuploadedf/';
//function for saving the uploaded images in a specific folder
move_uploaded_file($filetmpname, $folder.$filename);
//inserting image details (ie image name) in the database
$sql = "INSERT INTO `uploadedimage` (`imagename`) VALUES ('$filename')";
$qry = mysqli_query($conn, $sql);
if( $qry) {
echo "</br>image uploaded";
}
}
?>
The uploaded images are saved on a folder on my server calledimagesuploadedf
I used this code below to render the image on theresult.php
but it's showing the file name only, I need the image to be rendered.
$conn = new mysqli($servername, $username, $password, $dbname);
$sql = 'SELECT * from uploadedimage';
if (mysqli_query($conn, $sql)) {
echo "";
} else {
echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}
$count=1;
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
// output data of each row
while($row = mysqli_fetch_assoc($result)) { ?>
<tbody>
<tr>
<td>
<?php echo $row['imagename']; ?>
</td>
</tr>
</tbody>
<?php
$count++;
}
} else {
echo '0 results';
}
?>
</table>
What do I need to write onresult.php
to show/render the uploaded image on the page?
Answer
Solution:
You should write an HTML page containing referring to the images you want to display.
It might look like this:
<td>
<?php
$imgTag = '<img src="/imagesuploadedf/' . $row['imagename'] . '" />';
echo $imgTag; ?>
</td>
This puts out stuff like this into your web page.
<td>
<img src="/imagesuploadedf/image_file_name"/>
</td>
If your web page is coming fromhttps://images.example.com
this will make the browser download the image athttps://images.example.com/imagesuploadedf/image_file_name
from your server and display it in the page.
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: undefined array key php
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.