0

I am trying to upload an image on server using php in iphone.

Along with that i am passing an extra parameter with it.

when i am parsing that variable in php..it is not getting.

my code is

NSData *imageData = UIImageJPEGRepresentation(imageView.image, 90);
// setting up the URL to post to
NSString *urlString = @"http://www.announceworld.co.in/TravelApp/php/test-upload.php";

// setting up the request object now
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:@"POST"];


NSString *boundary = [NSString stringWithString:@"---------------------------14737809831466499882746641449"];
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
[request addValue:contentType forHTTPHeaderField: @"Content-Type"];

/*
 now lets create the body of the post
 */
NSMutableData *body = [NSMutableData data];

//Location
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"location\"\r\n\r\n%@", [place name]] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];

//Image
[body appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"userfile\"; filename=\"ipodfile.jpg\"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:@"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 the body of the post to the reqeust
[request setHTTPBody:body];

and my php file is

<?php  
$file_name = substr ( md5(uniqid(rand(),1)), 5, 15); 

$location = basename($_FILES['location']['name']);
$output = str_replace(" ","",$location);

$baseuploaddir = "/home/announce/public_html/TravelApp/images/$location/icons/";
$file = basename($_FILES['userfile']['name']);
$newname = $file_name . $file;
$uploadfile = $baseuploaddir . $newname;

if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
        $imagepath =  $baseuploaddir;// Give the path where the image saves. or print     some messages
}

?>

so its not able to get location..

so where am i going wroing...pls help me

Thanks

1
  • hello, Can you please explain to me what [place name] and imageView from imageView.image is? Just started learning about this.
    – Rick
    Apr 10, 2012 at 20:59

1 Answer 1

0

First try checking with the hardcoded name of image in your php file and check whether your service is working fine. And in you iOS code check whether you are using a correct content type because for image it should be something like image/png.

3
  • If i do Hardcoded as $baseuploaddir = "/home/announce/public_html/TravelApp/images/ABC/icons/"; Then it is uploading the image at proper location. But when i use $location, it says:- No such directory exists"/home/announce/public_html/TravelApp/images//icons/"; Jan 17, 2012 at 11:34
  • This means you are not able to give the proper path to the variable $location.Check how can you get the correct path from your ios client. Jan 17, 2012 at 11:58
  • I have shown above ios client code where i am putting the location value.. when i print here location value, its giving proper value Jan 17, 2012 at 12:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.