Displaying jpeg from temp dir

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
chrisw
Forum Newbie
Posts: 4
Joined: Tue Oct 25, 2005 11:23 pm

Displaying jpeg from temp dir

Post by chrisw »

Hi all!

I'm building a site where the user inputs various text data into a form. Also they browse for some pics (jpegs) and the whole lot is submitted to a script called review.php.

On this page the information they have submitted on the previous page is displayed as a completed ad (it's a car ad site). If the user is happy with how the ad looks they hit submit and the data is uploaded to a mysql db.

However the problem I have is displaying the images on this preview page. Obviously at this point the images have not been uploaded to the db so I can't run a script to get/display the image from a db quiery. At the point the preview.php script runs the image data is held in the server /tmp folder so I was under the impression I could just access this data however I'm having no luck. I'm using the simple img src= string to display the image. My plan was if I could just point the src= to the /tmp location of the file then all would be ok. The code is as follows...

src="<?echo $_FILES['img1']['tmp_name']?>

When the page is displayed, right clicking on the image box shows a path of http://www.mysite.co.uk/tmp/file which to my mind is correct. The question is can you read from the tmp folder in this way???

Also the /tmp folder on my server is not in my /httpdocs folder so I'm just wondering whether this dir is even accessable through a browser in the first place.

Thanks in advance!
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

Hi, firstly, don't use short tags, it's best to always use full <?php ?> tags :)

You would probably need to use the readfile() function, ala:

Code: Select all

<?php

readfile($_FILES['img1']['tmp_name']);

?>
But don't hold me to that, as I have not done this myself :)

Also input filtering, validation and security will need concideration to ensure you are displaying a picture and not something nasty :)
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post by onion2k »

You're right .. the tmp directory will be outside of your web accessible directory structure so the image won't be displayed. Just use move_uploaded_file() to put the file into a temporary directory that the user can see and put the filename in the session so you don't lose it, and you're sorted.
chrisw
Forum Newbie
Posts: 4
Joined: Tue Oct 25, 2005 11:23 pm

Post by chrisw »

Ok guys thanks for the suggestions!

I did suspect that the dir probably wasn't accessable but didn't know whether I should just move the /tmp location by changing the php.ini or just use some kind of temp dir on the site root and move the files to that.

The only problem with moving the file to another temp location is that it would presumably become permenant and I would have to delete these files at regular intervals. The other way I suppose I could do it would be to simply submit all the ad details and pics to the database during this preview script allowing me to use a simple getimage.php?id=$id kind of thing to display the images. If the user decided that the ad needs changing I can just take them back to the previous page, update the info and then over write the same db record. The only problem with this is if they decide during the preview that they don't actually want to submit the ad. I'll be stuck with an ad on the site that shouldn't be there!!

Maybe changing the /tmp location to the root would be the simplest way?? Is this a common thing to have to do or am I missing a simple solution to this problem!!?? :oops:
Post Reply