Hello I am trying to upload Image From my IOS device to server.
And Here Is my code to upload the Image
- (IBAction)btnUpload:(id)sender {
if (self.imageViewGallery.image == nil) {
UIAlertView *ErrorAlert = [[UIAlertView alloc] initWithTitle:@"Wait"
message:@"Please Select An Image To Upload." delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil, nil];
[ErrorAlert show];
NSLog(@"error");
}
else{
NSData *imageData = UIImageJPEGRepresentation(self.imageViewGallery.image, 90);
NSString *urlString = @"http://localhost/ColorPicker/api.upload.php";
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:@"POST"];
NSString *boundary = @"---------------------------14737809831466499882746641449";
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
[request addValue:contentType forHTTPHeaderField: @"Content-Type"];
NSString *imgName = LIbraryImage;
NSLog(@"image name : %@",imgName);
NSMutableData *body = [NSMutableData data];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"files\"; filename=\"%@\"\r\n", imgName]] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"Content-Type: application/octet-stream\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];
NSLog(@"setRequest : %@", request);
NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
NSLog(@"returnstring is : %@",returnString);
}
Now here is my server side code. It is a bit long.
<?php
$Image = $_FILES['files']['name'];
print_r($_FILES);
foreach ($Image as $f => $name) {
$allowedExts = array("gif", "jpeg", "jpg", "png");
$temp = explode(".", $name);
$extension = end($temp);
if ((($_FILES['files']['type'][$f] == "image/gif") || ($_FILES['files']['type'][$f] == "image/jpeg") || ($_FILES['files']['type'][$f] == "image/jpg") || ($_FILES['files']['type'][$f] == "image/png")) && ($_FILES['files']['size'][$f] < 2000000) && in_array($extension, $allowedExts)) {
if ($_FILES['files']['error'][$f] > 0) {
echo "Return Code: " . $_FILES['files']['error'][$f] . "<br>";
} else {
if (file_exists("upload/" . $name)) {
} else {
move_uploaded_file($_FILES['files']['tmp_name'][$f], 'upload/' . uniqid() . "_" . $name);
}
}
} else {
$error = "Invalid file";
}
}
?>
Now what happen every time I try to upload got this warning
<b>Warning</b>: Invalid argument supplied for foreach() in <b>/Applications/XAMPP/xamppfiles/htdocs/ColorPicker/uploadAction.php</b> on line <b>5</b><br />
From the comments, that
print_r
is returning:Meanwhile, your code is doing this:
You're treating
$_FILES['files']['name']
as if it's an array, when it isn't. That's why PHP says "Invalid argument supplied for foreach()"Please note that it can become an array if multiple files are uploaded with the same form element name. This is by far the most infuriating thing about
$_FILES
.Your uploading code does not attempt to use the PHP syntax needed to upload multiple files at once, so your PHP code does not need to worry about the content of the inner array being arrays themselves. You can eliminate the
foreach
loop and all references to the[$f]
index.When you upload image data as multipart, it end up in
$_FILES
. Since PHP 4.3.0,$_REQUEST
no longer contains any information about files uploaded in this manner.The rest of your code is based around the idea of the image not being uploaded as multipart but instead being uploaded as a base-64 string in a standard form. Instead of writing base 64 data to a file, you will simply need to get the information about the file from
$_FILES
and move it to your target directory withmove_uploaded_file
.