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

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']}\" />"

Related

Display image from database Codeigniter

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.

image File permission is not working

The file is created when the user registers successfully , each user folder thats created is only read/writable by daemon not by admin or anyone else,
the main users folder is created by me and has all permissions set that needs be and it doesnt even feature the name daemon
The problem I have is that, when I upload an image, it uploads to the directory of that user(the right directory of the users name) and I see it there but the problem is when I want to echo the image onto the page it does not work.
I use the moveupload() function and it moves but it is not able to show the image on the page, all other information is shown on the users page like name ect , but just not the image .What can I do?
This is the code snippet to create the file when user registers . I'm using a mac
if(!file_exists("user/$u")) {
mkdir("user/$u",0755);
}
Are you using proper HTML syntax for echo-ing your image? You can't simply do an echo $imagePath; in PHP, you must user proper HTML syntax for the output.
echo "<img src='".$imagePath."' />";
Edit:
If you're using HTML mixed with php, the HTML output would be as follows:
HTML
<img src="<?php echo $log_userpath . "/" . $main_userimage;?>" width="220px" height="300px" />

php image open using session user name as "username".jpg

this is my first time asking a question about this but unlike my other problems i have been unable to find the specific answer i am seeking. i am very new to php but it's working well on a current project.
i have this line of code which works perfectly in context of my page when the image file name is set to a specific name.
<?php echo $_SESSION['userName'];?>
<?php echo "<img src='videos/jpgs/'userName'.jpg' width='83'
height='20'/>"; ?>!
what i would like to do is open an image which uses the user name as the image filename. The images are in the folder in the path. The users are set internally and their user names are predetermined.
thanks in advance.
Use:
<?php echo "<img src='videos/jpgs/".$_SESSION['userName'].".jpg' width='83' height='20'/>"; ?>
<img src='videos/jpgs/<?php echo $_SESSION['userName']; ?>.jpg'/>
You could try this one too

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');

Categories