Its not a duplicate because the issue was NOT the undefined index.
I originally had the issue from my iOS app but I created an html form. It works now from html but not from iOS.
The form code is:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
<form enctype="multipart/form-data" action="uploadPhoto.php" method="POST">
Choose a file to upload: <input name="userfile" type="file" /><br />
<input type="submit" value="Upload File" />
</form>
</body>
</html>
and the php code for uploadPhoto.php is:
<?php
////////////////
echo 'file count=', count($_FILES),"\n";
var_dump($_FILES);
echo "\n";
////////////////
$uploaddir = './photos/';
if(isset($_FILES['userfile']['name'])){
$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');
$canupload = ini_get('file_uploads');
$tempdir = ini_get('upload_tmp_dir');
$maxsize = ini_get('upload_max_filesize');
echo "\r\n" . $_FILES['userfile']['size'] . "\r\n" . $_FILES['userfile']['type'] ;
}
?>
And I get this from a browser upload:
file count=1 array(1) { ["uploaded file"]=> array(5) { ["name"]=> string(9) "Koko2.jpg" ["type"]=> string(10) "image/jpeg" ["tmp_name"]=> string(14) "/tmp/phpH6pnPi" ["error"]=> int(0) ["size"]=> int(72727) } } Temp file not uploaded.
I do have 777 for that photos folder. I echoed and get: 1 for file_uploads 8M for post_max_size 128M for upload_max_filesize
The server error log says: User undefined index in uploadPhoto.php lines 18, 24 & 29 which refer to each mention of $_FILES['userfile']['name'].
Here is the iOS code:
NSData *imageData = UIImageJPEGRepresentation(image, 0.6);
// 0 Define URL
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"http://www.myservers.com/app/photos/uploadPhoto.php"]];
// 1 Create session configuration
NSURLSessionConfiguration *config = [NSURLSessionConfiguration defaultSessionConfiguration];
config.HTTPMaximumConnectionsPerHost = 1;
// 2 Create session & variables
NSURLSession *upLoadSession = [NSURLSession sessionWithConfiguration:config delegate:self delegateQueue:nil];
// 3 Create request & body & parameters
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url];
[request setHTTPMethod:@"POST"];
// 4 Create object to put content into...
NSMutableData *body = [NSMutableData data];
// 5 Create body of the request to put into the object
NSString *boundary = @"0xKhTmLbOuNdArY";
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
[request addValue:contentType forHTTPHeaderField:@"Content-Type"];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
NSString *filename = [NSString stringWithFormat:@"someName.jpg"];
[body appendData:[[NSString stringWithString:[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"userfile\" filename=\"%@\"\r\n",filename]] dataUsingEncoding:NSUTF8StringEncoding]];
//[body appendData:[@"Content-Type: application/octet-stream\r\n\r\n" 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]];
[request setHTTPBody:body];
// 3
self.uploadTask = [upLoadSession uploadTaskWithRequest:request fromData:body];
// 4
self.uploadView.hidden = NO;
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
// 5
[_uploadTask resume];
It only fails from my ios code. I was unable to find a similar issue in the 11 other answers mentioned in SO.
What could be the problem?