How to display images from a database using PHP? - php

My program needs to upload images, so an image (varchar) location is saved in a MySQL database. It's working so far.
Now I want to display images and this does not work. Here is the code:
include ('connect.php');
if(isset($_POST['submit'])){
$filetemp= $_FILES['image'] ['tmp_name'];
$filename= $_FILES['image'] ['name'];
$filepath= "images/".$filename;
move_uploaded_file($filetemp,$filepath);
$sql=mysqli_query($con,"insert into images (image) value ('$filepath')");
if($sql){
echo "uploaded";
}
else{
echo " not uploaded";
}
}
$sql=mysqli_query($con,"select * from images");
while($row=mysqli_fetch_array($sql)){
echo "<img src=' images/".$row['image']."'>"; // the problem is here, its just displaying img icon, not actual image
}
?>

remove images from image path, you already store this in image column in images table
echo "<img src='/".$row['image']."'>"; // the problem is here, its just displaying img icon, not actual image

Correction :
$sql=mysqli_query($con,"select * from images");
while($row=mysqli_fetch_array($sql)){
echo "<img src='".$row['image']."'>"; // the problem is here, its just displaying img icon, not actual image
}
Also you need to get the actual path via __FILE__ in case if needed.

You are adding images/ into the database then when you call the row to display the image you're adding images/ adding making it look in images/images/filename

Related

Alter profile picture using <img> tag and PHP

I'm still new on php. I'm currently building a website which a user can upload an image to change their profile picture.
The file of image will we insert into table user, row image location on database and directory "upload". The formatting is like abc.jpeg
My code for the <img> tag, which is where I display the profile image. But the image does not appear. Only the thumbnail image is displayed and not the exact image.
<img src=<?php
$current = $fgmembersite->UserEmail();
if ($handle = opendir('upload/')) {
$sql = "SELECT image_location FROM user WHERE email='$current'";
//$file = mysql_real_escape_string($sql);
if ((file_exists('upload/'.$sql) == $sql)) {
echo 'upload/'.$sql.'.png';
} else if (file_exists('upload/'.$sql)) {
echo 'upload/'.$sql.'.jpg';
}
}
closedir($handle);
?> alt="">
If you don't mind, can you check my code and see where I went wrong? Thank you for your helping.

Images from mysql does not display using php

Image is successfully fetching from database but it not be shown by the PHP. In place of image it shows a iamge thumbnail.
If I use header('Content-type:image/jpg'); it will show the only thumbnail all page contents disappear.
include 'functions/connect.php';
$user = $_SESSION['email'];
$sql = "SELECT photo FROM user WHERE email='$user'";
$run_sql = mysqli_query($conn,$sql);
$row = mysqli_fetch_array($run_sql);
$user_photo =$row['photo'];
echo"<p><img src='$user_photo'></p>";`
You can try something like this:
<img alt="<?php echo $user_photo; ?>" src="uploads/<?php echo $user_photo; ?>" /> // Upload is folder where image was uploaded
It depends on what the field "photo" contains. If the photo field contains the location of the picture on your system, then the way you have done it should work. However, if the field contains the actual photo in binary format (having field data type as 'blob') then you'll need to do the following:
file_put_contents("image.jpg", $user_photo);
echo "<p><img src='image.jpg' ></p>";

PHP Display all images from DB in a img tagg - PDO

Im trying to display alla images in a <img> from the db but i am only getting a broken image!
I am storing the ['tmp_name'] as LONGBLOB and the ['name'] as type in my tabel.
$query = $this->connection->prepare("SELECT * FROM images");
$query->execute();
$row = $query->fetchAll();
foreach($row as $img){
echo "<pre>";var_dump($img['name']);"</pre>";
echo "<img src=".$img["name"].">";
}
//header("Content-type: ".$row['type']);
print $row['name'];
die();
Should i use ['tmp_name'] or ['name'] in the <img>?
<img src=""> should include the full server path where your image is successfully uploaded. Also should include the image extension as well (.jpg, .png, .gif). should look like similar to following.
<img src="http://ichef.bbci.co.uk/images/ic/160xn/p0306tt8.png">
Easiest way is to check the source code of page and see if you get complete / correct path to the image. If not, check/change your code for missing part.

Image display with <img src=""> not working

EDIT :: I found out that i am having a permission error. For whatever reason i dont have "permission" to grab the image from the images folder ... *
a few days ago i asked this question about uploading images and was very satisfied with the anwser. I integrated it into my site and it works no problem.
Image - Upload not responding, no access to $_FILES
The problem i am having now is i cannot retrieve the images in any way shape or form. I have tried opening them and setting them to read, with fopen($image, r). I tried even more complicated things like loading the image back into a database ... then it hit me that the anwser is simple.
I CAN see the image in my images folder, so should work no problem. But it doesnt =(
Here is the PHP script one more time,
if (isset($_FILES['fupload'] ))
{
$max_size = mysql_real_escape_string(strip_tags($_POST['MAX_FILE_SIZE']));
$file = $_FILES['fupload']['name'];
if(isset($max_size) && !empty($max_size) && !empty($file)) {
$file_type = $_FILES['fupload']['type'];
$tmp = $_FILES['fupload']['tmp_name'];
$file_size = $_FILES['fupload']['size'];
$allowed_type = array('image/png', 'image/jpg', 'image/jpeg', 'image/gif');
if(in_array($file_type, $allowed_type)) {
if($file_size < $max_size) {
$path ='images/'.$file;
$move = move_uploaded_file($tmp, $path);
$sql = "UPDATE info.profile SET Profile_pic='".$move."', pic_name='".$file."' WHERE Nick='".$_SESSION['Nick']."'";
mysql_query($sql) or die ("Error: " .mysql_error());
$_SESSION['pic'] = $path;
echo "image added successfully :) :) :) ";
}
I left out all the else statements, since they are not relevent. The folowing is an example of where the image should be displayed.
<td rowspan=2 align=center width=100px> <img src="<?php $_SESSION['pic']; ?>"
alt="Here would be the picture, if it was working !!"> </td>
The alt is a small teaser of course, but i have tried everything and with the current script i managed to get the alt text to display. Sometimes a "bad image" icon displayes (like when you open images in an email before allowing images to be displayed)
When i echo the path it shows the "right" path, just like i would enter when manualy giving it in. The reason i store the path in DB and in a session is because the image is a profile image and should only be displayed for THAT user ... wouldnt want other users having other images as their profile pic. =)
Did you cross check the file permission issue?
What is the response code return when you copy and paste the image path to the browser?
e.g. http://yourserver.com/part-to-image/image.jpg
If 404 returned, you may need to troubleshoot the server directory mapping.
If 500 returned, might be file permission.
Apart from that your code
<?php $_SESSION['pic']; ?>
Should it require echo to embed to html tag?
<?php echo $_SESSION['pic']; ?>
The path should be relative to the current directory. Have u checked it ?
Try the answer given in comment by Rohit.
<img src='<?php echo $_SESSION['pic']; ?>' />

Read multiple image php

I have two separate files, one is to display the html/php document image, and the other is a php file that renders the image using the header function content-type:image/jpeg.
I tried using it with one image and it works well. However, I need to display multiple images. How could I do this?
The html/php doc has an img tag that points out to the php file that renders the image
echo "<image src=Image.php>";
The image.php
$selectimage = mysql_query("SELECT Image from ImageTbl", $con);
if($selectimage)
{
header("Content-type:image/jpeg");
while($row = mysql_fetch_array($selectimage))
{
echo $row["Image"];
}
}
make two files one for image another for fetching the row like this
image.php
$image_id = $_GET["id"];
header("Content-type:image/jpeg");
//query database to get only one image from id
echo $row["Image"];
another file
getimages.php
//query for image data
while($row = mysql_fetch_array())
{
echo "<img src='image.php?id=$row[id]' />";
}
You can't output all the images together, because to the browser, it will look like the data of multiple images mushed together, which is nonsensical. Also, each image tag can only display one image. To solve this, give the image table an ID field to identify the image.
Then in the file that outputs HTML, do something like this (passing the ID for the image you need):
echo "<image src='Image.php?id=1>";
echo "<image src='Image.php?id=2>";
echo "<image src='Image.php?id=3>";
And then in the file that outputs the image, do:
$id = intval($_REQUEST['id']); // intval will validate the ID to be an int
$selectimage = mysql_query("SELECT Image from ImageTbl WHERE id=$id LIMIT 1", $con);
if ($selectimage) {
$row = mysql_fetch_array($selectimage);
if ($row) { // check if the image really exists
header("Content-type:image/jpeg");
echo $row["Image"];
}
}
Use a foreach loop to loop through the requested records and echo them out independently to the img tags which your using.
That would be the best way in my opinion.
If you want to display number of different images using one script, try to add some unique hash to the script name ( for e.g. md5( microtime() ) )
$seed = md5( microtime() );
echo '<image src="Image.php' . $seed . '">';

Categories