{"id":3362,"date":"2014-03-23T11:23:41","date_gmt":"2014-03-23T11:23:41","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/03\/23\/receiving-an-image-in-php-via-http-post-request-from-ios-collection-of-common-programming-errors\/"},"modified":"2014-03-23T11:23:41","modified_gmt":"2014-03-23T11:23:41","slug":"receiving-an-image-in-php-via-http-post-request-from-ios-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/03\/23\/receiving-an-image-in-php-via-http-post-request-from-ios-collection-of-common-programming-errors\/","title":{"rendered":"Receiving an image in php via http post request from iOS-Collection of common programming errors"},"content":{"rendered":"<ul>\n<li><img decoding=\"async\" src=\"http:\/\/i.stack.imgur.com\/trYiy.png?s=32&amp;g=1\" \/><br \/>\npka2012<\/p>\n<p>I am trying to send an image from an iOS device to a php script. I am not getting an error on the iOS side, so I believe the problem is with my php script. I am new to php so please forgive me. Here is the php script:<\/p>\n<pre><code>\n<\/code><\/pre>\n<p>And in case anyone knows much about iOS, here&#8217;s the code I use to send the image:<\/p>\n<pre><code>\/\/ Dictionary that holds post parameters. You can set your post parameters that your server accepts or programmed to accept.\nNSMutableDictionary* _params = [[NSMutableDictionary alloc] init];\n[_params setObject:[NSString stringWithString:@\"1.0\"] forKey:[NSString stringWithString:@\"ver\"]];\n[_params setObject:[NSString stringWithString:@\"en\"] forKey:[NSString stringWithString:@\"lan\"]];\n\n\/\/ the boundary string : a random string, that will not repeat in post data, to separate post data fields.\nNSString *BoundaryConstant = [NSString stringWithString:@\"----------V2ymHFg03ehbqgZCaKO6jy\"];\n\n\/\/ string constant for the post parameter 'file'. My server uses this name: `file`. Your's may differ\nNSString* FileParamConstant = [NSString stringWithString:@\"file\"];\n\n\/\/ the server url to which the image (or the media) is uploaded. Use your server url here\nNSURL* requestURL = [NSURL URLWithString:@\"http:\/\/myurl.com\/upload.php\"];\n\n\n\/\/ create request\nNSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];\n[request setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData];\n[request setHTTPShouldHandleCookies:NO];\n[request setTimeoutInterval:30];\n[request setHTTPMethod:@\"POST\"];\n\n\/\/ set Content-Type in HTTP header\nNSString *contentType = [NSString stringWithFormat:@\"multipart\/form-data; boundary=%@\", BoundaryConstant];\n[request setValue:contentType forHTTPHeaderField: @\"Content-Type\"];\n\n\/\/ post body\nNSMutableData *body = [NSMutableData data];\n\n\/\/ add params (all params are strings)\nfor (NSString *param in _params) {\n    [body appendData:[[NSString stringWithFormat:@\"--%@\\r\\n\", BoundaryConstant] dataUsingEncoding:NSUTF8StringEncoding]];\n    [body appendData:[[NSString stringWithFormat:@\"Content-Disposition: form-data; name=\\\"%@\\\"\\r\\n\\r\\n\", param] dataUsingEncoding:NSUTF8StringEncoding]];\n    [body appendData:[[NSString stringWithFormat:@\"%@\\r\\n\", [_params objectForKey:param]] dataUsingEncoding:NSUTF8StringEncoding]];\n}\n\n\/\/ add image data\nNSData *imageData = UIImageJPEGRepresentation(finalImage, 1.0);\nif (imageData) {\n    [body appendData:[[NSString stringWithFormat:@\"--%@\\r\\n\", BoundaryConstant] dataUsingEncoding:NSUTF8StringEncoding]];\n    [body appendData:[[NSString stringWithFormat:@\"Content-Disposition: form-data; name=\\\"%@\\\"; filename=\\\"image.jpg\\\"\\r\\n\", FileParamConstant] dataUsingEncoding:NSUTF8StringEncoding]];\n    [body appendData:[[NSString stringWithString:@\"Content-Type: image\/jpeg\\r\\n\\r\\n\"] dataUsingEncoding:NSUTF8StringEncoding]];\n    [body appendData:imageData];\n    [body appendData:[[NSString stringWithFormat:@\"\\r\\n\"] dataUsingEncoding:NSUTF8StringEncoding]];\n}\n\n[body appendData:[[NSString stringWithFormat:@\"--%@--\\r\\n\", BoundaryConstant] dataUsingEncoding:NSUTF8StringEncoding]];\n\n\/\/ setting the body of the post to the reqeust\n[request setHTTPBody:body];\n\n\/\/ set the content-length\nNSString *postLength = [NSString stringWithFormat:@\"%d\", [body length]];\n[request setValue:postLength forHTTPHeaderField:@\"Content-Length\"];\n\n\/\/ set URL\n[request setURL:requestURL];\n<\/code><\/pre>\n<p>I found both of this on stackoverflow, but from my understanding the iOS is working properly and the php should place the photo in the root directory on my server.<\/p>\n<\/li>\n<li><img decoding=\"async\" src=\"http:\/\/www.gravatar.com\/avatar\/5df5cddcb9570cb603c2b288f71ed3d9?s=32&amp;d=identicon&amp;r=PG\" \/><br \/>\nRay Paseur<\/p>\n<p>The $_FILES array holds much useful information. You can (and should) use var_dump() to print out the contents. Here is a script that works to upload files. Maybe you can adapt the design for your own needs.<\/p>\n<pre><code>\n    <\/code><\/pre>\n<h2><code>Upload file(s)<\/code><\/h2>\n<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>pka2012 I am trying to send an image from an iOS device to a php script. I am not getting an error on the iOS side, so I believe the problem is with my php script. I am new to php so please forgive me. Here is the php script: And in case anyone knows [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-3362","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/3362","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=3362"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/3362\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=3362"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=3362"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=3362"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}