Bellek PHP Dosya yükleme

5 Cevap php

Ben, bir html formundan bir dosya yüklemek PHP post it ve bir dosyaya yazma atlayarak, belleğe yüklemek istiyorum. Bir dosya yükleme yapmak ve bellekte tutmak, ya da ben bir dosyaya yazmak zorunda mümkün mü?

5 Cevap

Ben bu artık mümkün olduğunu sanmıyorum. Bir akışından dosyasını açarak dışladı, böylece enctype = "multipart / form-data" ile php :/ / girişi kullanamazsınız. Eğer sonrası yol giderseniz, sadece diskteki dosyaya herhangi bir ikili veri içermiyor $ _file değişkeni, sadece işaretçi kullanabilirsiniz.

Xforms (http://www.ibm.com/developerworks/xml/library/x-xformstipuploadphp/index.html) yardımcı olacak gibi görünüyor ama bu bile Firefox 3.5 olduğunu. Bu plug-in, benim için bir anlaşma katil gerektirir.

PUT opsiyonu serin. POST kullanmak ve $ _FILES istedim, ben alabileceğin en yakın bir ramdisk'in de upload_tmp_dir işaret etmek olacağını düşünüyorum.

PHP gerçek HTTP isteklerini kendisi (ki web sunucunun işi) işlemez beri bu mümkün olduğunu hayal bile edemiyorum. Biri olsa beni yanlış kanıtlamak olabilir eğer harika olurdu.

Http koymak PHP kolaydır.

<?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.

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)

İstemci tarafında,

ile başlık göndermek "Content-Type: application / octet-stream;" yerine "multipart / form-veri"

böylece yükleme dosya sunucusunda geçici dir içine tasarruf olmaz.

Örneğin ios objektif-c kodu almak:

    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];

sunucu tarafında,

(php :/ / input "multipart / form-data" çalışmaz unutmayın) ham veri olarak dosya almak için "php :/ / girişi" kullanımı

örneğin: $data = file_get_contents('php://input');

Burada $ veri yüklenen dosya olacaktır.


Bizim durumumuzda, dosya yükleme sıklığı oldukça yüksektir. Bu yüzden posible olarak dosya sistemi işlemi azaltmak için iyidir.