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');
Related
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 !!.
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" />
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.
There is an image inside HTML page. I use Jquery for 360 grad rotation. My Jquery plugin gets the list of all images via AJAX request. But the problem is, that I don't know where the user will locate scripts ang images.
For example images will be located at home/mypage/public_html/gallery/pictures/ and scripts at home/mypage/public_html/scripts/rotate/. I have an option for user to show the path to scripts and want the script to get images path automatic, based on first image path.
For example I have some image with relative path:
<img src='pictures/001.jpg'>
url is: mypage.com/gallery/pictures/001.jpg
How can I get a path to containing folder in form:
home/mypage/public_html/gallery/pictures/
It's no matter to do it with Jquery or PHP
a PHP script then loads all images from home/mypage/public_html/gallery/pictures/
Thx!
<img src='<?php echo $_SERVER['DOCUMENT_ROOT'] ?>/gallery/pictures/001.jpg'>
try this
In the end it depends on whether you process your data on the server side (PHP) or on the client side (JavaScript), but you need to use the getcwd() PHP function:
http://il.php.net/manual/en/function.getcwd.php
So you can pass the relative image path to PHP through jQuery, and then append the current working directory (getcwd) to the beginning.
How about this:
$_SERVER['DOCUMENT_ROOT'];
Path of mypage.com\ plus gallery\pictures plus Image name
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']}\" />"