Advertise here




Advertise here

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In with Google Sign In with OpenID
Please do not post the same thing multiple times. The board software automatically flags certain posts as needing moderator attention. This happens the most often for new users. I'm pretty sure this is made clear at the time you attempt to post. Posting the same thing over and over again just makes that many more posts the moderators have to weed through later. This makes us sad. Don't make us sad. If your post/thread doesn't appear, just wait a while. Don't post it again. If it hasn't shown up by the next day, then you can try again. I normally go through posts in the mornings, and try to check a few times throughout the day, but I'm not here 24/7. There will typically be a significant delay before posts are approved. Just be patient.

Problems while trying to upload image to server.

I'm stucked in this problem since days and i really need your help.

I'm actually trying to upload a file from my iphone app to a webserver trough a php script.

this is my method in Xcode:
-(void)sendPost{
UIImageView *imageView = [[UIImageView alloc] init];
imageView.image = [UIImage imageNamed:@"back.png"];

NSData *imageData = UIImageJPEGRepresentation(imageView.image, 90);
NSString *urlString = @"myscriptishere.php";
NSMutableURLRequest *request = [[NSMutableURLRequest alloc]init];
[request setTimeoutInterval:60.0];
[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"];

NSMutableData *body = [NSMutableData data];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];

[body appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"userfile\";filename=\"myfile.png\"\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]];

[request setHTTPBody:body];
NSLog(@"%@",[[NSString alloc] initWithData:[NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil] encoding:NSUTF8StringEncoding]);
// Do any additional setup after loading the view.

}
and my php code is:
<?php<br /> $domainpath = $_SERVER['DOCUMENT_ROOT'];
$target = "./upload";
$public = "/public/upload/";
$target = $target.basename( $_FILES['userfile']['name']) ;
$check = getimagesize($_FILES['userfile']['tmp_name']);

if (move_uploaded_file($_FILES["file"]["tmp_name"],
$domainpath. $public . $_FILES["file"]["name"]))
echo "YES";

else
echo "NO";


?>
I always receive (after few seconds, so I guess I'm uploading something) the server reply "NO".

I already checked I have permission on the folder I'm going to write in.

Any ideas on where the problem could be hidin?

Thanx in advance

Replies

  • GHuebnerGHuebner Posts: 553Registered Users
    I had an issue with the boundary when trying to upload. I was uploading to a .net web service and not php. So, this may not work for you. My code stripped out a lot of what yours has and it is working. Here is how mine looked after I got it to work... My service expected a URL in the format:
    http://www.domain.com/service1.svc/UploadPhotoOne/filename/jpg"
    This is the format that I send as the url. The only thing I send in the body is the image data. I would save your original code and then test adding back in the other data elements within the body until you can get it to work.


    -(void)sendPost
    {
    UIImageView *imageView = [[UIImageView alloc] init];
    imageView.image = [UIImage imageNamed:@"back.png"];

    NSData *imageData = UIImageJPEGRepresentation(imageView.image, 90);

    NSString *urlString = @"myscriptishere.php";

    NSMutableURLRequest *request = [[NSMutableURLRequest alloc]init];

    [request setTimeoutInterval:60.0];
    [request setURL:[NSURL URLWithString:urlString]];
    [request setHTTPMethod:@"POST"];

    NSString *contentType = [NSString stringWithFormat:@"text/plain"];
    [request addValue:contentType forHTTPHeaderField:@"Content-Type"];

    NSMutableData *body = [NSMutableData data];

    [body appendData:[NSData dataWithData:imageData]];
    [request setHTTPBody:body];

    [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];

    // Do any additional setup after loading the view.

    }
Sign In or Register to comment.