CLJ
CLJ

Reputation: 1927

PHP File upload to memory

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?

Upvotes: 6

Views: 7402

Answers (6)

awiebe
awiebe

Reputation: 3836

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.

Upvotes: 0

Towolf
Towolf

Reputation: 81

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.

Upvotes: 2

Licson
Licson

Reputation: 155

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.

Upvotes: 0

CLJ
CLJ

Reputation: 1927

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.

Upvotes: 2

Mat Schaffer
Mat Schaffer

Reputation: 1704

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.

Upvotes: 2

Kai
Kai

Reputation: 9552

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.

Upvotes: -1

Related Questions