1

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?

6
  • Do you know the name of the image file? Commented Nov 20, 2010 at 9:42
  • @pekka, an img tag won't work here as it needs to display the file in real time, the one that is uploaded. Commented Nov 20, 2010 at 9:49
  • oh @gligoran, i don't know the file name, as it needs to display the one uploaded by the user. The file is stored in a folder that has only one image. Commented Nov 20, 2010 at 9:50
  • it is like the way facebook's profile pictures work. Commented Nov 20, 2010 at 9:50
  • @jack: u don't have to know the file name. that's the reason why your img tag should refer to a php variable pointing to the location of the file! i think u misunderstand php and its usage in dynamic sites altogether. Commented Nov 20, 2010 at 10:09

4 Answers 4

2

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.

Sign up to request clarification or add additional context in comments.

Comments

0
<img src="/path/to/the/upload/folder/<?php echo $filename; ?>"/>

or:

echo '<img src="/path/to/the/upload/folder/'.$filename.'"/>";

4 Comments

an img tag won't work here as it needs to display the file in real time, the one that is uploaded.
every user has a specific folder with one picture in it. I want it to display that picture. By the way, what is $filename?
$filename is the name of the file derived from $_FILES['the_name_of_the_html_element_specified_in_the_upload_form']['tmp_name']
Thanks for explaining, but I am getting an error that says undefined index :file....file is the name specified.
0

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.

Comments

0
<?php
  header('Content-Type:image/jpeg');
  readfile('path_to_your/image.jpg');

2 Comments

@kemp, yeah that's exactly what I mean.
@brad can you show the code of how have you displayed the picture that the user just uploaded ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.