php - How to insert image into remote database by iOS app? -
in ios app, have store image in remote database. have done blob already.but want storing path only. have tried lot failing write client side code.
client side code:
nsdata *imagedata = uiimagejpegrepresentation(self.imgview.image,0.2); //change image nsdata if (imagedata != nil) { nsstring *filenames = @"catttt.jpeg"; //set name here nslog(@"%@", filenames); nsstring *urlstring = @"http://localhost/insertintoimage.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"]; nsmutabledata *body = [nsmutabledata data]; [body appenddata:[[nsstring stringwithformat:@"\r\n--%@\r\n",boundary] datausingencoding:nsutf8stringencoding]]; [body appenddata:[[nsstring stringwithformat:@"content-disposition: form-data; name=\"filenames\"\r\n\r\n"] datausingencoding:nsutf8stringencoding]]; [body appenddata:[filenames datausingencoding:nsutf8stringencoding]]; [body appenddata:[[nsstring stringwithformat:@"\r\n--%@\r\n",boundary] datausingencoding:nsutf8stringencoding]]; [body appenddata:[@"content-disposition: form-data; name=\"userfile\"; filename=\".jpeg\"\r\n" 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]]; // setting body of post reqeust [request sethttpbody:body]; // lets make connection web nsdata *returndata = [nsurlconnection sendsynchronousrequest:request returningresponse:nil error:nil]; nsstring *returnstring = [[nsstring alloc] initwithdata:returndata encoding:nsutf8stringencoding]; nslog(@"%@",returnstring); nslog(@"finish"); }
remote side code:
$target_path = "uploads/"; $target_file = $target_path . basename( $_files['userfile']['filenames']); if(move_uploaded_file($_files['userfile']['tmp_name'], $target_file)) { echo "the file ". basename( $_files['userfile']['filenames']) . " has been uploaded"; } else { echo "there error uploading file, please try again!"; } ?>
first upload image server , update image name/path db
below function store image server
here $image imagedata comes ios app
$image = $_post['image_name_from_app']; $image = str_replace("<", "", $image); $image = str_replace(">", "", $image); $image = str_replace("<", "", $image); $image = str_replace(">", "", $image); $image = preg_replace('/[\s\w]+/', '', $image); $bin = hex2bin($image); $path = "user_images/"; $imagename = time() .".png"; $path = "user_images/"; if (file_put_contents($path . $imagename, $bin)) { return $imagename; } return false;
Comments
Post a Comment