7

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?

1
  • 1
    It seems too much power required to the humble PHP. Commented Oct 16, 2009 at 20:04

6 Answers 6

3

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Are you expecting people to be able to upload files using a web browser? Because you may have difficulty finding browsers that support PUT.
An alternative is to use a server-side implementation of XForms (for instance the open source 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.
2

I faced the problem recently and finally use the following solution: (not sure if it's actually solve the problem, I will really appreciate your suggestion)

on Client side,

send header with "Content-Type : application/octet-stream;" instead of "multipart/form-data"

so the uploading file won't save into temp dir on server.

take ios objective-c code for example:

    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
    [request setURL:[NSURL URLWithString:@"your server address"]];
    [request setTimeoutInterval:30];
    [request setHTTPMethod:@"POST"];

    // set Content-Type in HTTP header
    NSString *contentType = [NSString stringWithFormat:@"application/octet-stream;"];
    [request setValue:contentType forHTTPHeaderField: @"Content-Type"];

    // post body
    NSMutableData *body = [NSMutableData data];

    //yourData is typed NSData here
    [body appendData:yourData]; 

    // setting the body of the post to the reqeust
    [request setHTTPBody:body];

    // set the content-length
    NSString *postLength = [NSString stringWithFormat:@"%d", [body length]];
    [request setValue:postLength forHTTPHeaderField:@"Content-Length"];


    AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id result) {
            success(result);  
    } failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id result){
        failure(error, result);
    }];
    [operation start];

on Server side,

use "php://input" to retrieve file as raw data ( note that php://input won't work on "multipart/form-data")

for example: $data = file_get_contents('php://input');

$data here will be the uploaded file.


In our case, file uploading frequency is pretty high. So it's better to reduce filesystem operation as posible.

Comments

1

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.

1 Comment

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

You could point the upload dir to a tmpfs volume if under linux, then it will be in memory, even though using the filesystem interface.

Comments

0

Http put is easy in PHP.

<?php
$f = fopen('php://input','r');
$content ='';
while(!feof($f)){
    $content .= fread($f,8192);
}
?>

$content is the content of file. Remember to config the web server first to handle HTTP PUT request.

Comments

-1

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.

3 Comments

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.