Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I also wanted to upload an image along with other data in the same POST, most information about uploading images is just for uploading an image alone in one server connection, here is how I solved this.</p> <p>This is the PHP</p> <pre><code>&lt;?php /* As you can see there are more values not only the image */ $variableOne = $_POST['variableOne']; $variableTwo = $_POST['variableTwo']; $variableThree = $_POST['variableThree']; $variableFour = $_POST['variableFour']; $variableFive = $_POST['variableFive']; $variableSix = $_POST['variableSix']; $variableSeven = $_POST['variableSeven']; $variableEight = $_POST['variableEight']; $variableNine = $_POST['variableNine']; $variableTen = $_POST['variableTen']; /* Our image */ $image = $_REQUEST['image']; /* This is for trying to get a unique name for the image file, since maybe you want to store large amount of images */ $currentDate = date("Y-m-d"); $name = "" . $currentDate . microtime() . rand(0, 999) . rand(0, 999) . rand(0, 999) . ".jpg"; /* * Here comes the image stuff */ if (file_exists($name)) { echo "File already exists"; } else { /* Decoding image */ $binary = base64_decode($image); /* Opening image */ $file = fopen($name, 'wb'); /* Writing to server */ fwrite($file, $binary); /* Closing image file */ fclose($file); echo "Added"; } } ?&gt; </code></pre> <p>Then in Xcode copy and paste this method (taken from <a href="https://stackoverflow.com/questions/6006823/creating-a-base-64-string-from-nsdata">Creating a base-64 string from NSData</a>)</p> <pre><code>- (NSString*)base64forData:(NSData*) theData { const uint8_t* input = (const uint8_t*)[theData bytes]; NSInteger length = [theData length]; static char table[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/="; NSMutableData* data = [NSMutableData dataWithLength:((length + 2) / 3) * 4]; uint8_t* output = (uint8_t*)data.mutableBytes; NSInteger i; for (i=0; i &lt; length; i += 3) { NSInteger value = 0; NSInteger j; for (j = i; j &lt; (i + 3); j++) { value &lt;&lt;= 8; if (j &lt; length) { value |= (0xFF &amp; input[j]); } } NSInteger theIndex = (i / 3) * 4; output[theIndex + 0] = table[(value &gt;&gt; 18) &amp; 0x3F]; output[theIndex + 1] = table[(value &gt;&gt; 12) &amp; 0x3F]; output[theIndex + 2] = (i + 1) &lt; length ? table[(value &gt;&gt; 6) &amp; 0x3F] : '='; output[theIndex + 3] = (i + 2) &lt; length ? table[(value &gt;&gt; 0) &amp; 0x3F] : '='; } return [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding]; } </code></pre> <p>In YourViewController.h file </p> <pre><code>@interface YourViewController : UIViewController { NSURLConnection *serverConnection; NSMutableData *returnData; } </code></pre> <p>This is the server connection code</p> <pre><code>NSURL *sendURL = [NSURL URLWithString:@"http://yourdomainname/imagefolder/phpscript.php"]; NSMutableURLRequest *sendRequest = [NSMutableURLRequest requestWithURL:sendURL cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:30]; [sendRequest setHTTPMethod:@"POST"]; [sendRequest setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"]; NSData *imageData = UIImageJPEGRepresentation(yourImage, 1.0); NSString *encodedString = [[self base64forData:imageData] stringByReplacingOccurrencesOfString:@"+" withString:@"%2B"]; NSString *dataToSend = [[NSString alloc] initWithFormat:@"variableOne=%@&amp;variableTwo=%@&amp;variableThree=%@&amp;variableFour=%@&amp;variableFive=%@&amp;variableSix=%@&amp;variableSeven=%@&amp;variableEight=%@&amp;variableNine=%@&amp;variableTen=%@&amp;image=%@", valueOne, valueTwo, valueThree, valueFour, valueFive, valueSix, valueSeven, valueEight, valueNine, valueTen, encodedString]; [sendRequest setHTTPBody:[dataToSend dataUsingEncoding:NSUTF8StringEncoding]]; serverConnection = [[NSURLConnection alloc] initWithRequest:sendRequest delegate:self]; [serverConnection start]; </code></pre> <p>Set delegate methods for server connection</p> <pre><code>- (void) connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response { returnData = [[NSMutableData alloc] init]; } - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data { [returnData appendData:data]; } - (void)connectionDidFinishLoading:(NSURLConnection *)connection { if (connection == serverConnection) { NSString *responseString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding]; NSLog(@"Response: %@", responseString); if ([responseString isEqualToString:@"Added"]) { /* Make something on success */ } else { /* Make something else if not completed with success */ } } } - (void) connection:(NSURLConnection *)connection didFailWithError:(NSError *)error { /* Make something on failure */ } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload