php – AFNetworking上传图像
发布时间:2020-12-13 14:01:52  所属栏目:PHP教程  来源:网络整理 
            导读:我看了几个例子,但我认为我的问题可能在 PHP中.我正在尝试使用AFNetworking从iPhone上传图像到服务器.这是我的对象代码: -(IBAction)uploadButtonClicked:(id)sender{NSData *imageToUpload = UIImageJPEGRepresentation(mainImageView.image,90);AFHTTPCli
                
                
                
            | 
                         
 我看了几个例子,但我认为我的问题可能在 
 PHP中.我正在尝试使用AFNetworking从iPhone上传图像到服务器.这是我的对象代码: 
  
  
  
-(IBAction)uploadButtonClicked:(id)sender
{
NSData *imageToUpload = UIImageJPEGRepresentation(mainImageView.image,90);
AFHTTPClient *client= [AFHTTPClient clientWithBaseURL:[NSURL URLWithString:@"http://www.THESERVER.com"]];
NSMutableURLRequest *request = [client multipartFormRequestWithMethod:@"POST" path:@"/PROJECT/upload.php" parameters:nil constructingBodyWithBlock: ^(id <AFMultipartFormData>formData) {
    [formData appendPartWithFileData: imageToUpload name:@"file" fileName:@"temp.jpeg" mimeType:@"image/jpeg"];
}];
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation,id responSEObject) {
    NSString *response = [operation responseString];
    NSLog(@"response: [%@]",response);
    [MBProgressHUD hideHUDForView:self.view animated:YES];
} failure:^(AFHTTPRequestOperation *operation,NSError *error) {
    [MBProgressHUD hideHUDForView:self.view animated:YES];
    if([operation.response statusCode] == 403){
        NSLog(@"Upload Failed");
        return;
    }
    NSLog(@"error: %@",[operation error]);
}];
[operation start];
} 
 这是我的upload.php: function upload(){
    $uploaddir = '/uploads/';
    $file = basename($_FILES['file']['name']);
    $uploadfile = $uploaddir . $file;
    if (move_uploaded_file($_FILES['file']['tmp_name'],$uploadfile)) {
        sendResponse(200,'Upload Successful');
        return true;
    }
    sendResponse(403,'Upload Failed');
        return false;
} 
 当我尝试上传它失败 if (move_uploaded_file($_FILES['file']['tmp_name'],$uploadfile)) { 
 并返回我设置的403 / false状态代码 
 这是一个愚蠢的错误… 
  
                          在php我需要 $uploaddir = 'uploads/'; 代替 $uploaddir = '/uploads/'; (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!  | 
                  
