Pergunta

Hi I'm trying to upload Audio/Video files to a server by using a PHP. It uploads correctly for files under 1MB, but for files bigger than 1MB starts to fail. I achieved some uploads of 1.5MB or even 2MB, but never bigger. Any idea why this is happening? Here my PHP code:

$folder = "../audio/";

if (is_uploaded_file($_FILES['audio']['tmp_name']))  {   
if (move_uploaded_file($_FILES['audio']['tmp_name'], $folder.$_FILES['audio']['name'])) {
    Echo "File uploaded";
} else {
    Echo "File not moved to destination folder. Check permissions";
};
} else {
    Echo "File is not uploaded.";
}; 

Here my iOS Code:

    NSData *audioData = [[NSData alloc] initWithContentsOfURL:recordedFile];
    NSString *prefixString = @"Audio";

    NSString *guid = [[NSProcessInfo processInfo] globallyUniqueString] ;
    NSString *uniqueFileName = [NSString stringWithFormat:@"%@_%@", prefixString, guid];

    NSString *urlString = @"http://www.myserver.com/uploadaudio.php";
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
    [request setURL:[NSURL URLWithString:urlString]];
    [request setHTTPMethod:@"POST"];

    NSString *boundary = @"_187934598797439873422234";
    NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
    [request setValue:contentType forHTTPHeaderField: @"Content-Type"];
    [request setValue:@"text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8" forHTTPHeaderField:@"Accept"];
    [request setValue:@"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_5) AppleWebKit/536.26.14 (KHTML, like Gecko) Version/6.0.1 Safari/536.26.14" forHTTPHeaderField:@"User-Agent"];
    [request setValue:@"http://google.com" forHTTPHeaderField:@"Origin"];

    NSMutableData *body = [NSMutableData data];
    [body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"audio\"; filename=\"%@.mp3\"\r\n", uniqueFileName] dataUsingEncoding:NSUTF8StringEncoding]];

    [body appendData:[@"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];

    [body appendData:[NSData dataWithData:audioData]];

    [body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];


    [request setHTTPBody:body];
    [request addValue:[NSString stringWithFormat:@"%lu", (unsigned long)[body length]] forHTTPHeaderField:@"Content-Length"];


    NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
    NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
    NSLog(@"%@", returnString);
Foi útil?

Solução

Make sure the php.ini settings allow for larger uploads as well:

PHP change the maximum upload file size

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top