views:

218

answers:

3

I would like to upload a file from an html form, post it to PHP and load it into memory, bypassing writing it to a file. Is it possible to do a file upload and keep it in memory, or do I have to write it to a file?

A: 

Since PHP doesn't handle the actual HTTP requests itself (that's the web server's job) I can't imagine that this is possible. It would be awesome if somebody could prove me wrong though.

Kai
definitely not possible using standard PHP procedures.
dusoft
PHP does have a setting "upload_tmp_dir", which controls where the uploaded files go. So it seems like it does have some control.
JW
Yeah that's interesting. I'd imagine that it just tells Apache, "yo, put those guys over here!"
Kai
+1  A: 

I do not think this is possible now. You can not use php://input with enctype="multipart/form-data", so that rules out opening the file from a stream. If you go the post route, you only can use the $_FILE variable, which does not contain any binary data, just the pointer to the file on disk.

It looks like xforms will help (http://www.ibm.com/developerworks/xml/library/x-xformstipuploadphp/index.html) but this is in even Firefox 3.5. It requires a plug-in, which is a deal killer for me.

Chris J
Are you expecting people to be able to upload files using a web browser? Because you may have difficulty finding browsers that support PUT.
Frank Farmer
http://stackoverflow.com/questions/165779/are-the-put-delete-head-etc-methods-available-in-most-web-browsers
Frank Farmer
An alternative is to use a server-side implementation of XForms (for instance the open source http://www.orbeon.com/). This way, you can support all browsers without requiring people to install extensions, and you can handle upload in the way you describe. Of course, IMHO this only makes sense if you also need other features from XForms, in addition to the file upload.
Alessandro Vernet
+1  A: 

The PUT option is cool. If you wanted to use POST and $_FILES, I think the closest you could get would be to point upload_tmp_dir at a ramdisk.

matschaffer
I'm cool with put, does anyone have a complete example of doing this with put?
Chris J