开发者

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 memor开发者_开发问答y, or do I have to write it to a file?


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.


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.


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.


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.


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜