Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- I am trying to upload an image from my device to the server and each time the else condition executes... Please help me sending the right request from device to server
- <?php
- include 'colorsofimage.php';
- include 'include/functions.php';
- $base = $_REQUEST["image"];
- if (isset($base)) {
- $image_name = "img_" . date("Y-m-d-H-m-s") . ".jpg";
- $binary = base64_decode($base);
- // binary, utf-8 bytes
- header("Content-Type: bitmap; charset=utf-8");
- $file = fopen("images/post_images/" . $image_name, "wb");
- if($file){
- echo 'sucess';
- }else{
- echo 'error';
- }
- die();
- fwrite($file, $binary);
- fclose($file);
- mysql_connect('localhost', 'root', '');
- mysql_select_db('colorsearch');
- mysql_query("INSERT INTO image(name)VALUE('$image_name')");
- $last_id = mysql_insert_id();
- $src = 'images/post_images/' . $image_name;
- $functions = new ColorsConversion();
- $image = new ColorsOfImage($src);
- $colors = $image->getProminentColors();
- $background = $image->getBackgroundColor();
- foreach ($colors as $color) {
- // mysql_query("INSERT INTO image_color(image_color,image_id) VALUE('$color','$last_id')");
- $rgb = $functions->hex2rgb($color);
- $hsv = $functions->RGBtoHSV($rgb[0], $rgb[1], $rgb[2]);
- //
- // $rgb = hex2rgb($color);
- // $hsv = RGBtoHSV($rgb[0], $rgb[1], $rgb[2]);
- mysql_query("INSERT INTO image_color(image_color,image_background,image_color_r,image_color_g,image_color_b,image_color_h,image_color_s,image_color_v,image_id) VALUE('$color','$background','$rgb[0]','$rgb[1]','$rgb[2]','$hsv[0]','$hsv[1]','$hsv[2]','$last_id')");
- }
- die($image_name);
- } else {
- die("No POST");
- }
- ?>
- And here is my code for uploading 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"];
- NSMutableData *body = [NSMutableData data];
- [body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
- [body appendData:[@"Content-Disposition: form-data; name=\"userfile\"; filename=\"iphonefile.jpg\"\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]];
- [request setHTTPBody:body];
- NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
- NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
- NSLog(@"returnstring is : %@",returnString);
- if (returnString != nil) {
- UIAlertView *uploadAlert = [[UIAlertView alloc] initWithTitle:@"Hooray!"
- message:@"Your Image is uploaded" delegate:nil
- cancelButtonTitle:@"OK"
- otherButtonTitles:nil, nil];
- [uploadAlert show];
- }
- else
- {
- UIAlertView *ErrorAlert = [[UIAlertView alloc] initWithTitle:@"Oops"
- message:@"Image Couldn't uploaded." delegate:nil
- cancelButtonTitle:@"OK"
- otherButtonTitles:nil, nil];
- [ErrorAlert show];
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment