tags:

views:

1679

answers:

4

Can you store an image in a PHP SESSION ?

I have a multi-step registration process using PHP on my site. On one of the steps, the users can upload their company logo (image).

The last step is to process their credit card.

So before I write any images to the web server and image location to the database, I want to make sure their credit card is valid and process.

As such, is it possible to temporarily store that image data in the SESSION variable?

If not, how else do people temporaily store image data on forms before committing that data?

Thanks in advance.

+8  A: 

You can but expect memory usage of your session to increase depending on the size of the images. In order to do so, you must save the file contents into a session variable.

If it is in session data and you have multiple steps after the upload the image will be reloaded (into the session) every page view until the steps are complete.

I would personally recommend against using the session for holding a binary file. Saving the image on disk into a temporary location until the registration is complete. I would only save the path to the temporary file in session. When the transaciton is completed move it to a proper location and do your db inserts.

Also, in essence, session data is stored on disk (or db) anyway so you might as well save the image file once then issue a move command once complete.

Darryl E. Clarke
A: 

Yes, you can store an image in a PHP session. Get it into PHP as a string (i.e. binary data) and then you can put it in the session.

You will want it to only be as big as it needs to be and you need to delete it as soon as you don't need it because large pieces of information in the session will slow down the session startup.

staticsan
+1  A: 

When a file is uploaded, it gets assigned a temporary name in the $_FILES array. I don't know the exact lifespan of those files, but you might be able to capture that name in the session and put off the move_uploaded_file() call until after the CC is verified.

Or, you could do the CC step first.

davethegr8
+4  A: 

I'd save the file to disk, you could even name it using the user's session id. Then there could be some sort of clean up script which is run as a cron job and deletes the images of people who never successfully paid.

If you try and store an image in the session, you're doing it wrong.