PHP code works fine. Ive downloaded files from an html form on the same server. The downloaded files ranged from 40K to 2.0M, so its size was not. File upload is activated on the server since PHP 5.3
I found a lot of posts like this one (no response yet): https://stackoverflow.com/questions/19710388/using-nsurlsession-to-upload-an-image .
This one uses uploadTaskWithRequest: fromFile: instead of fromData: NSURLSession make sure the upload is done
This is NSURLSessionDataTask instead of uploadTaskWithRequest: No data reception with NSURLSession
And some messages that seem to say that uploadTaskWithRequest: fromData: just doesn't work:
NSURLSession: loading assets with background
wrapping asynchronous loading using NSURLSession will not work, but synchronous NSURLConnection does
as well as
upload an image from iOS to PHP
I made the application in such a way that it returns an HTTP code, and I get the code 200. There is nothing in the error_log server file after loading. Everything seems to be working fine, but no matter what happens, the file is not being written to the server. Any ideas what else I can try to figure out what is going wrong?
Here is the php code:
<?php
$file='';
$uploaddir='';
////////////////
echo 'file count=', count($_FILES),"\n";
var_dump($_FILES);
echo "\n";
////////////////
if(isset($_FILES['userfile']['name'])){
$uploaddir = './photos/'; //Uploading to same directory as PHP file
$file = basename($_FILES['userfile']['name']);
echo "file is set";
$uploadFile = $file;
$randomNumber = rand(0, 99999);
$newName = $uploaddir . $uploadFile;
if (is_uploaded_file($_FILES['userfile']['tmp_name'])) {
echo "Temp file uploaded. \r\n";
} else {
echo "Temp file not uploaded. \r\n";
}
if (move_uploaded_file($_FILES['userfile']['tmp_name'], $newName)) {
$postsize = ini_get('post_max_size'); //Not necessary, I was using these
$canupload = ini_get('file_uploads'); //server variables to see what was
$tempdir = ini_get('upload_tmp_dir'); //going wrong.
$maxsize = ini_get('upload_max_filesize');
echo "\r\n" . $_FILES['userfile']['size'] . "\r\n" . $_FILES['userfile']['type'] ;
}
}
?>
and here is the iOS code:
- (void)uploadImage:(UIImage*)image {
NSData *imageData = UIImageJPEGRepresentation(image, 0.6);
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"http://www.myserver.com/app/photos/uploadPhoto.php"]];
NSString *boundary = @"0xKhTmLbOuNdArY";
NSString *filename = [NSString stringWithFormat:@"someName.jpg"];
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url];
[request setHTTPMethod:@"POST"];
NSURLSessionConfiguration *config = [NSURLSessionConfiguration defaultSessionConfiguration];
NSURLSession *upLoadSession = [NSURLSession sessionWithConfiguration:config delegate:self delegateQueue:nil];
NSMutableData *body = [NSMutableData data];
[request addValue:contentType forHTTPHeaderField:@"Content-Type"];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"userfile\" filename=\"%@\"\r\n",filename]] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"Content-Type: image/jpeg\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[NSData dataWithData:imageData]];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
__block NSString *stringForText = @"Hola";
self.uploadTask = [upLoadSession uploadTaskWithRequest:request fromData:body completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse*) response;
int errorCode = httpResponse.statusCode;
NSString *errorStatus = [NSString stringWithFormat:@"%d",errorCode];
NSString *responseString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSString *totalResponse = [errorStatus stringByAppendingString:responseString];
stringForText = totalResponse;
[self updateView:stringForText];
self.uploadView.hidden = NO;
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
}];
[_uploadTask resume];
}
-(void)updateView:(NSString*)texto{
dispatch_sync(dispatch_get_main_queue(), ^{
self.myTextView.text = texto;
});
}
I have 777 permissions in this folder. As I said, Ive been able to download and thus write to this folder from the same php script via the html form.
TotalResponse - 200 http count = 0, (0) {} php.
: 
Temp:
$body = $HTTP_RAW_POST_DATA;
echo "\n REQUEST" . $body;
:
file count=1 array(1) { ["user file"]=> array(5) { ["name"]=> string(12) "DrinkCO2.png" ["type"]=> string(9) "image/png" ["tmp_name"]=> string(14) "/tmp/phpxg7oLZ" ["error"]=> int(0) ["size"]=> int(190550) } } file is setTemp file uploaded. REQUEST 190550 image/png