5

I'm building a multi-page form with PHP. I'm storing most of the value's in $_SESSION. Using the following method...

$_SESSION['title'] = $_POST['title'];

It's working fine so far but I'm not sure if it will be able to hold PDFs, videos, ect.

Does the $_SESSION only hold strings & intigers, or can files be stored there?

Philip Kirkbride
  • 21,381
  • 38
  • 125
  • 225

5 Answers5

7

The maximum size of a $_SESSION is the maximum memory allowed to a PHP script, but chances are if you even get to 1/3 of that, you're doing something wrong.

$_SESSION, as a rule, should only be used to keep what information is needed by the user for the majority of the site page views. It is highly improbable, that you will actually need a file on each page.

Here's a better option, store a temp file on the disk and assign the path to the temp file in the $_SESSION. Then, when you need it, read the file to the user.

cwallenpoole
  • 79,954
  • 26
  • 128
  • 166
  • I'm reading that the tmpfile() is automatically removed at the end of the php script? Wouldn't that make it not possible for use with a 'multi-page' form? – Philip Kirkbride Oct 05 '11 at 20:50
  • Create a file named by the `PHPSESSID` and have a cron job clean them up after a given interval. – cwallenpoole Oct 05 '11 at 20:59
5

I do not suggest you to store big value because this information is stored in your server. You should serialize a PHP object that will contain path to those file if you want to keep some reference to these files.

Edit: The session is loaded into memory at run time, so in theory it's limited by the memory_limit in php.ini.

Patrick Desjardins
  • 136,852
  • 88
  • 292
  • 341
2

I'm not sure whether you would be able to store an entire file into $_SESSION, but you could upload your file to the server and store only a path to the file.

JanLikar
  • 1,296
  • 9
  • 22
1

By default the sessions are stored as files on the server. The only limit opposed is the PHP script memory limit - eg 64MB, which can be controlled. Files can be stored in both binary and text format but I would question the need to do so - after all, why would you do that?

ddinchev
  • 33,683
  • 28
  • 88
  • 133
0

$_SESSION is stored on server in the folder where it defined in the php.ini

so as long as your server has space you can store anything on the $_SESSION

Eli Y
  • 877
  • 16
  • 41