is it possible to store $_FILE into $_SESSION

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
jonwondering
Forum Commoner
Posts: 39
Joined: Mon Mar 13, 2006 6:26 pm

is it possible to store $_FILE into $_SESSION

Post by jonwondering »

Does anybody know if it is possible to store $_FILE into $_SESSION successfully. I am trying to get rid of $_POST variables... I store everything into session, instead of having it in $_POST array on a form. That way when the user hits the Refresh button, it won't give that screen/message that page has expired. But I don't know how to handle the form that uploads images. Is it even possible to get rid of post variables ($_file) in that form?

thanks.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Re: is it possible to store $_FILE into $_SESSION

Post by Chris Corbyn »

jonwondering wrote:Does anybody know if it is possible to store $_FILE into $_SESSION successfully. I am trying to get rid of $_POST variables... I store everything into session, instead of having it in $_POST array on a form. That way when the user hits the Refresh button, it won't give that screen/message that page has expired. But I don't know how to handle the form that uploads images. Is it even possible to get rid of post variables ($_file) in that form?

thanks.
You'll want to write a function to do it. The steps are:

1. Read $_FILES['foo']['tmp_name']
2. Use move_uploaded_file() to copy the file to your own tmp location
3. Switch $_FILES['foo']['tmp_name'] to be that new location
4. Copy $_FILES to the session

References will bail on something like this so just do a plain copy.
Gambler
Forum Contributor
Posts: 246
Joined: Thu Dec 08, 2005 7:10 pm

Post by Gambler »

I recommend you to simply use GET. IIRC, you can stick files into get variable on modern browsers.

Note: If you use Firefox, it carries post variables through refresh.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

Gambler wrote:I recommend you to simply use GET. IIRC, you can stick files into get variable on modern browsers.

Note: If you use Firefox, it carries post variables through refresh.
Hmm.... how does GET transport file data? Does the data appear in the URL? (I thought URL's had a maximum length). :)
jonwondering
Forum Commoner
Posts: 39
Joined: Mon Mar 13, 2006 6:26 pm

Post by jonwondering »

I see what you are saying d11wtq, I did the same thing you recommend except I converted $_file into $_session before uploading the image... that must've been the mistake :( I'll try it that way. Thanks.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

jonwondering wrote:I see what you are saying d11wtq, I did the same thing you recommend except I converted $_file into $_session before uploading the image... that must've been the mistake :( I'll try it that way. Thanks.
One thing to bear in mind. Once you've moved the file the move_uploaded_file() and is_uploaded_file() functions will not work correctly on the new file.
Gambler
Forum Contributor
Posts: 246
Joined: Thu Dec 08, 2005 7:10 pm

Post by Gambler »

I thought URL's had a maximum length
Not in RFC. Although, I can't find any documents that say how exactly you should upload through GET, so this might be just my imagination.
jonwondering
Forum Commoner
Posts: 39
Joined: Mon Mar 13, 2006 6:26 pm

Post by jonwondering »

i decided to go around the problem. once the form uploads the image(s), it redirects to itself one time, that way all $_POST and $_FILE variables are gone. all the information i needed, i stored when i was processing the images. thanks for your help d11wtq and Gambler.
Post Reply