Reputation: 811
I want to upload images in server using ASIHTTPRequest library in my IPhone app.
For image upload, i am using ASIFormDataRequest to upload into server.
I have tried the below codes in my app but it couldn't working and image didn't uploaded in server.
NSURL *url = [NSURL URLWithString:@"http://www.anglerdemo.com/Appln/Aghaven_iphone/Uploadphoto.php"];
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
request.requestMethod = @"POST";
NSString *fileName = @"iphone.jpg";
[request addPostValue:fileName forKey:@"name"];
// Upload an image
UIImage *img = [UIImage imageNamed:fileName];
NSData *imageData = UIImageJPEGRepresentation(img, 90);
NSLog(@"imageData ==> %@", imageData);
[request setData:imageData withFileName:fileName andContentType:@"image/jpeg" forKey:@"image"];
[request setDelegate:self];
[request startAsynchronous];
I have tried above codes in my app, request successfully executed and i got the alert message in "requestFinished" delegate method of ASIHTTPRequest. but image didn't uploaded in server.
- (void)requestFinished:(ASIHTTPRequest *)request
{
[[[[UIAlertView alloc]
initWithTitle:@"Message"
message:@"Success!!!"
delegate:self
cancelButtonTitle:@"OK"
otherButtonTitles:nil]
autorelease]
show];
NSLog(@"success ==> ");
}
I have tried php file script for upload is below.
<?php
$uploaddir = 'upload_files/';
$file = basename($_FILES['image']['name']);
$uploadfile = $uploaddir . $file;
if(move_uploaded_file($_FILES['image']['tmp_name'], $uploadfile))
{
echo "Photo has been saved successfully!…;
}
else {
echo "Failed";
}
?>
Please help in this regards.
Thanks!!!
Upvotes: 0
Views: 3298
Reputation: 61
I think the first thing I would do is add
NSLog(@"%@", [request responseString]);
to requestFinished: just to be sure you see "Failed" being logged. This will prove the problem is with the attempt to call move_upload_file, which I would guess is where things go wrong.
If that's the case, I would be pretty sure that the user your webserver runs as doesn't have write permissions to the upload_files directory.
Upvotes: 1