image not uploading to server here is my code
(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info
{
UIImage * chosenImage = [info valueForKey: UIImagePickerControllerOriginalImage];
[picker dismissViewControllerAnimated:YES completion:NULL];
[_userImage setImage:chosenImage];
NSData *imageData = UIImageJPEGRepresentation(_userImage.image,0.2);
NSString * string =[imageData base64EncodedStringWithOptions:kNilOptions];
if (imageData != nil)
{
NSString *filename = @"imageoooo";
NSString * requestURL = @"http://localhost/webservices/myfile.php";
NSMutableURLRequest * request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:requestURL]];
[request setHTTPMethod:@"POST"];
NSString *boundary = @"---------------------------14737809831466499882746641449";
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
[request addValue:contentType forHTTPHeaderField: @"Content-Type"];
NSMutableData *body = [NSMutableData data];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"IdLabel\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[_IdLabel.text dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"photo\"; filename=\"%@.jpg\"\r\n", filename] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[NSData dataWithData:imageData]];
[request setHTTPBody:body];
// set the content-length
NSString * postLength = [NSString stringWithFormat:@"%lu", (unsigned long)[body length]];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
// set URL
// [request setURL:requestURL];
[[[NSURLSession sharedSession] dataTaskWithRequest:request completionHandler:
^(NSData * data, NSURLResponse * response, NSError * error)
{
NSLog(@"%@",data);
NSString * myString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"Request reply: %@", myString);
dispatch_async(dispatch_get_main_queue(), ^{
NSDictionary * items = [NSJSONSerialization JSONObjectWithData:data
options:0
error:nil];
NSString * successstr =[NSString stringWithFormat:@"%@", [items valueForKey:@"success"]];
NSLog(@"%@", successstr);
});
}]
resume];
}
and my php code
<?php
require_once("config.php");
$response = array();
echo "<br>".$uploadDirectory = "http://localhost/makeintern/profile";
echo "<br>".$_POST["IdLabel"];
echo "<br>".$_FILES['photo']['name'];
if($_POST["IdLabel"] && $_POST["photo"]){
echo "<br>".$uploadDirectory = "http://localhost/makeintern/profile";
$errors = [];
$fileExtensions = ['jpeg','jpg','png'];
echo "<br>".$fileName = $_FILES['photo']['name'];
echo "<br>".$fileSize = $_FILES['photo']['size'];
echo "<br>".$fileTmpName = $_FILES['photo']['tmp_name'];
echo "<br>".$fileType = $_FILES['photo']['type'];
echo "<br>".$fileExtension = strtolower(end(explode('.',$fileName)));
}
echo json_encode($response);
return json_encode($response);
mysqli_close($con);
?>
error:
Undefined index: photo in /Applications/XAMPP/xamppfiles/htdocs/webservices/test-file.php on line 13
thanks in advance