I am using POST method and multipartMethod to upload image from iphone to server. My Objective-C code is
__weak AuthAPIClient *client = [AuthAPIClient sharedClient];
[client setParameterEncoding:AFJSONParameterEncoding];
[client registerHTTPOperationClass:[AFHTTPRequestOperation class]];
[SVProgressHUD showWithStatus:@"Saving data to server..."];
[SVProgressHUD show];
if ([self isInternetAvailable]) {
NSString *userId =[[AppSettings sharedAppSettings] getUserName];
NSString*replacedName=[userId stringByReplacingOccurrencesOfString:@" " withString:@"-"];
NSDictionary *parametersDic = @{@"user_name":userName,@"image_data":[NSString stringWithFormat:@"uploads/%@.jpeg",replacedName]};
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:parametersDic options:0 error:nil];
id json = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:nil];
/////////////////////////////Test code////////////////////////////////
NSData *imageToUpload = UIImageJPEGRepresentation(imagedata, 1.0);///imagedata is an UIImage passing in parameter of method.
NSMutableURLRequest *request = [client multipartFormRequestWithMethod:@"POST" path:@"update_profile.php" parameters:json constructingBodyWithBlock: ^(id <AFMultipartFormData>formData) {
[formData appendPartWithFileData: imageToUpload name:@"file" fileName:[NSString stringWithFormat:@"%@.jpeg",userId] mimeType:@"image/jpeg"];
}];
/////////////////////////////////////////////////////////////////////
NSLog(@"response request %@",request);
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
[operation setUploadProgressBlock:^(NSUInteger bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToWrite) {
NSLog(@"Sent %lld of %lld bytes", totalBytesWritten, totalBytesExpectedToWrite);
}];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject)
{
// [SVProgressHUD dismiss];
NSString *responseString = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
NSLog(responseString);
if (responseString != nil && ![responseString isEqualToString:@""] && operation.response.statusCode == 200) {
NSError *error;
// NSDictionary *responseDic = [NSJSONSerialization JSONObjectWithData:responseObject options:kNilOptions error:&error];
NSMutableArray* jsonArray = [NSJSONSerialization JSONObjectWithData:responseObject options:kNilOptions error:&error];
NSLog(@"hello response %@",responseString);
}
else {
NSLog(@"hello response failr %@",responseString);
if ([self.delegate respondsToSelector:@selector(serviceHelperSavePersonalInfoDataWithDetailsFailed:error:)]) {
[self.delegate serviceHelperSavePersonalInfoDataWithDetailsFailed:self error:@"No Response Recieved."];
}
}
}
failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error");
// [SVProgressHUD dismiss];
NSMutableString *errorMsg = [[NSMutableString alloc] init];
[errorMsg appendString:@"Error Occured"];
if(operation.response == nil)
{
[errorMsg appendString:@"Service not available. Please try again"];
}
else if (operation.response.statusCode == 500) {
[errorMsg appendString:@"Service not available. Please try again"];
}
else if (operation.response.statusCode == 403) {
[errorMsg appendString:@"Request forbidden. Please try again"];
}
else {
NSData *jsonData = [operation.responseString dataUsingEncoding:NSUTF8StringEncoding];
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:jsonData
options:0
error:nil];
if (json != nil) {
[errorMsg appendString:[json objectForKey:@"message"]];
}
}
[errorMsg appendString:@" service is not available."];
if ([self.delegate respondsToSelector:@selector(serviceHelperSavePersonalInfoDataWithDetailsFailed:error:)]) {
[self.delegate serviceHelperSavePersonalInfoDataWithDetailsFailed:self error:errorMsg];
}
}];
[operation start];
}
else {
if ([self.delegate respondsToSelector:@selector(serviceHelperSavePersonalInfoDataWithDetailsFailed:error:)]) {
[self.delegate serviceHelperSavePersonalInfoDataWithDetailsFailed:self error:@"Internet not available."];
}
}
And my PHP file(update_profile.php) code is
<?php
require_once "DatabaseConnection.php";
$myfile = fopen("newfile.txt", "w") or die("Unable to open file!");
$txt = "not set";
if(isset($_FILES)){
$txt = serialize($_FILES);
$result = "hellll";
}
fwrite($myfile, $txt);
fclose($myfile);
$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;
}
I have used my same code in other project that working good but not working in this project.Does it matter with EL Capitan OSX? Because i have updated my OS. Anyone have any idea where i am wrong? Any help would be appreciated. Thanks.
Error is: Data and image not recieved at server side.Nothing comes in isset($_FILES) and i have also check $_POST and $_REQUEST but server not recieving anything only "a:0{}" saving in 'newfile.txt'. i am creating file 'newfile.txt' at server but nothing comes in this file.