从ios上传大文件

时间:2014-10-14 17:15:36

标签: php ios

我正在尝试上传一个大文件,这会给我带来麻烦。 当我使用较小的文件上传时,工作正常。

以下是我正在使用的IOS中的代码:

NSURL * url = [NSURL URLWithString:urlString];

ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
[request setPostFormat:ASIMultipartFormDataPostFormat];
[request setPostValue:key forKey:@"key"];
[request setData:fileData withFileName:fileName andContentType:contentType forKey:key];
[request setRequestMethod:@"POST"];
[request setTimeOutSeconds:60000];
[request startSynchronous];

NSError *error = [request error];
if (!error)
{
    NSString *response = [request responseString];
    if (kShowLog)
        NSLog(@"%@", response);
    return [self objectWthString: response];
}
else
    if (kShowLog)
        NSLog(@"%@", error.debugDescription);
    return nil;

这是PHP代码:

function microtime_float_string()
{
    list($usec, $sec) = explode(" ", microtime());
    return $sec . ($usec * 1000000);
}

$myTime = microtime_float_string();
srand(time());
$rand1 = rand();
srand($rand1);
$rand2 = rand();
$str = $myTime . $rand1 . $rand2 . '.aiff';

$arr = array();

$uploaddir = 'images/';      //Uploading to same directory as PHP file

//$file = basename($_FILES['userfile']['name']);
$file = $str;
$uploadfile = $uploaddir . $file;

if (!is_uploaded_file($_FILES['userfile']['tmp_name']))
{
    $arr["error"] = "File Upload Failed is_uploaded_file";
}
else if ($_FILES['userfile']['size']> 10000000)     //Limiting image at 10000K
{
    $arr["error"] = "File Size too Large";
}
else if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) 
{
    $url = SITE_URL . $uploadfile;
    $arr["message"] = "ok";
    $arr["url"] = $url;
}
else
{
    $arr["error"] = "File Upload Failed";
}
echo json_encode($arr);

1 个答案:

答案 0 :(得分:0)

默认情况下,OSX中的php接受 2 mb

我们必须更新 php.ini 文件才能接受更大尺寸的文件。

该文件位于 / etc /

修改 upload_max_filesize &的值php.ini中的 post_max_size

然后通过运行以下命令重新启动它

/etc/init.d/httpd restart