Display image from database Codeigniter - php

i store profile images in database by adding this code to upload process
($config['upload_path'] = './img/photos/';
$data['img'] = $config['upload_path'].$image_data['file_name'];).
In my database there is img column that has such paths
./img/photos/8f8eeb37d5fd82b7ba4cd16a356064ce.jpg.
how can i display image in my view file? Thanks in advance.

Add this path with your base_url(). In your view file:
$url = base_url().$img;
<img src="<?=$url?>" />
You can also check your URL in browser before putting it in image tag.

echo $image_data['file_name'];
see what you get then add missing path .

For more dynamic, I would suggest to change
'./img/photos/';
to
'img/photos/';
It would put your file in the same directory then your file path would be nicer "img/photos/8f8eeb37d5fd82b7ba4cd16a356064ce.jpg"
To display your image you could :
$url = base_url().$image_path; //http://domain.com/img/photos/8f8eeb37d5fd82b7ba4cd16a356064ce.jpg
<img src="<?php echo $url;?>"/>

Do not store image with path in your database table.
Store only image name in your img column of the table.
For path, you can make one constant.
Form that constant, you can retrieve image from the path.

Related

img src not working for some cases

what i'm basically doing is to save some images within some directory of my localhost, the problem is when i try to show them. But here's the thing:
In my directory i have two exact images, named differently, one is named based on a unique name generated, and the other is named as it would normally be named.
For example:
Image No. 1 is called: o9z2z2f545faf1d1.jpg
Image No. 2 is called: testImage.jpg
When i want to show them i normally do it this way
<img src = "<?php echo $prize['Prize']['imageUrl']; ?>">
If i inspect the element i get "http://localhost:8080/admin/img/prizes/o9z2z2f545faf1d1.jpg"
If i do it this way it doesnt show anything.
But if i do this
<img src = "../../img/prizes/testImage.jpg">
And then if i inspect the element it gives: "http://localhost:8080/admin/img/prizes/testImage.jpg"
I check on the folder where the images are saved, and they look fine. its when i get to call them by the unique name that fails
How can i solve this?
Thank you very much!!
Try use the base tag in the head tag or absolute path of image.
<img src = "http://localhost:8080/admin/img/prizes/testImage.jpg">

Reviewing uploaded images with PHP and MySQL

I am using PHP to upload images in my web application. The images are stored in some directory on the server while their paths are stored directly in MySQL database.
The upload goes very well and images get in the folder but the problem is accessing thoses images with their path field stored in the database : i am not yet able to find the correct form of the path i should use, now am using the realPath and dirname functions to help me get the path so finally an example of path is C:\wamp\www\webroot\img.png (since am on Windows using wampserver) So when i do something like :
<img src="$image->path" />
i get no image shown in the browser and when i inspect it i get the expected code like :
<img src="C:\wamp\www\webroot\img.png" />
which means that this path format is not correct to show the image.
I have tried many things : i took the same path and acced it with the browser and it showed me the image (with the file protocol automatically) so i added file:// to the image path but nothing was new. I have also tried to acces it as a web url and with that it goes will for example it shows the image when putting
localhost/webroot/img.png
But what i need exactly is being able to store and retrive the image file again. So is it a file system probelm ? is the code platform independant ?
I will be very grateful for any help
Thank you.
You can't show an image with its path, but with its URL.
In PHP, we use to save the filename only in MySQL and after, you display the image with <img src="path/to/img/<?php echo $image->filename; ?>" />
That's all!
Add the file protocal to your path.
<img src="file:///<?php echo $image->path; ?>" />.
The best thing you could have to done is save the image in a folder inside your project/website folder. Then save the path to the image from the root folder in the DB.
Example: I uploaded a an image of filename "image.png" inside "Img" folder in the Website Root Folder. Then I will save "Img/image.png" inside my DB.
So anytime I want to reference the image, I will just use
<img src="/<?php echo $image->path; ?>" />
Or
<img src="<?php echo $image->path; ?>" />
Sorry for wasting your time, i have solved the problem. It was an issue with how my application is accessed. It should be accessed from only a directory called web so every path should be referenced from that directory !!.

PHP File upload (Images)

Just a quick questions guys. I'm using a 'browse' upload function within a form to select and upload a picture to my database. At the moment my images are in a 'Image' folder and to get them to show I need the following within my Image column:
Images\picname1.jpg
When I use the browse upload function, I get the following:
E:\xampp\htdocs\IDB\images\picname1.jpg
which is the displayed on the database as:
picname1.jpg
And of course as the 'Image' folder directory isn't included, the image will not display.
This is my form:
<font face="Arial"><b>Enter Image(Optional):</b></font><input type="file" name="imaAdd" accept="image/gif">
Could anyone tell me how I can include the 'Image directory!? I'm sure its an obvious fix I'm missing!
Any help would be great!
Edit:
Hers my Image echo guys.
echo"<img src='".$row->Image."'>";
One option would be to concatenate the string 'images/' with the image filename upon insertion into your database.
A better option would probably be to qualify the filename with the images path when you go to display the image. If you can show us the code you use to display the images, it should be an easy fix.
The PHP code for this latter option could look like:
echo '<img src="images\' . $imagename . '" />';
I'm slightly confused by what you are asking, correct me if im wrong but this is what you have:
An upload system that uploads pictures to your image directory and adds imageName.jpg to your database. And you want to prefix 'images\' to each of the image names added to your database?
If thats the case, in your php script that the form triggers you should be able to set the image name as added to the db as 'image\.$imagename' assuming $imagename as the name of the image.
And if that does not work why not prefix it while accessing the img? print '<img src=image\"'.$imagename.'" />
Hope that helps.

How to retrieve image from server and display using json parsing?

I have created a form which takes in images into a folder. I have only one image per folder and I want to display the image from the specified folder.
Say I've uploaded one picture to folder name uploads. Now I want to retrieve that image from the folder and display it.
How do I do that?
When a file is uploaded with your form, it becomes a file on your server, immediately. The web server puts it in a temporary directory and PHP tells you where it was put through the $_FILES array. You can immediately access this file, you can move it somewhere within your website's documents, and you can immediately print out an <img> tag pointing to where the file was put.
Stop writing "an img tag won't work" as that is exactly how you display the image.
Read the PHP manual page "Handling file uploads":
http://php.net/manual/en/features.file-upload.php
The PHP manual should always be the first place you go when you're trying to do something you haven't done before.
<img src="/path/to/the/upload/folder/<?php echo $filename; ?>"/>
or:
echo '<img src="/path/to/the/upload/folder/'.$filename.'"/>";
First you should have the image and path (or user) names in PHP variables. Then use an IMG tag to display them like :
<img src="some_path/uploads/<?php echo $username . '/' . $imagename; ?>">
you can't do it without knowing the name of the image file.
<?php
header('Content-Type:image/jpeg');
readfile('path_to_your/image.jpg');

How to turn an image's file path into the actual image?

I've saved a user's respective picture in a folder named 'profileportraits'. So when a user uploads their picture, it is saved into that folder. Furthermore, I also save the 'portrait path', i.e. the location/name of the photo onto a MySQL database for user data. My question is: I am able to echo the Portrait Path, where the user's portrait is stored onto the page. But how will I display the actual photo onto the page? I.e. how can I turn the file path into the actual picture? Will I have to match the path that I retrieved from MySQL to the directory? or something else? Below is a bit of code.
{
echo $row['PortraitPath'];
}
With the code above, I am able to echo the actual path, which is: profileportraits/DSC00310.JPG. I simply want to turn this path of the picture, into the actual picture. Thank you.
<img src="<?php echo $row['PortraitPath']; ?>" />
or
<?php
echo "<img src=\"{$row['PortraitPath']}\" />";
?>
Why not use the path as the source of an image tag:
echo "<img src=\"{$row['PortraitPath']}\" />"

Categories