【问题标题】:Upload Files to server bigger than 1MB / iOS将文件上传到大于 1MB / iOS 的服务器
【发布时间】:2014-04-25 18:50:13
【问题描述】:

您好,我正在尝试使用 PHP 将音频/视频文件上传到服务器。 对于 1MB 以下的文件,它可以正确上传,但对于大于 1MB 的文件,它会开始失败。我实现了一些 1.5MB 甚至 2MB 的上传,但从来没有更大。知道为什么会这样吗?这是我的 PHP 代码:

$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.";
}; 

这是我的 iOS 代码:

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

【问题讨论】:

  • 我认为,这个问题不是关于 iOS 的,而是关于 Apache 或其他你用作网络服务器的东西。 iOS 代码看起来很正常,但是服务端不是那么清晰。
  • 这是一个 PHP 配置问题。搜索一下如何使用 PHP 支持大文件上传。这与 iOS 无关。

标签: php ios audio upload hosting


【解决方案1】:

确保 php.ini 设置也允许更大的上传:

PHP change the maximum upload file size

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-11-16
    • 1970-01-01
    • 1970-01-01
    • 2012-08-10
    • 1970-01-01
    • 1970-01-01
    • 2016-08-01
    • 2012-07-06
    相关资源
    最近更新 更多