0

I have been working on an XCode project and I need some server-side code to go along with the Objective C. All I am trying to do is upload an image to the server.

I am currently using XAMPP to use my PHP file, and the image I am trying to upload should be going to localhost folder 'hopeImages'. However, upon running the PHP in my Chrome browser, I receive several messages reading:

Notice: Undefined index: userfile in...

Here is the Objective C, 'img' is defined elsewhere; I receive no errors in this code.

- (void)heyMethod
{
    NSData *imageData = UIImagePNGRepresentation(img);
    NSString *urlString = @"http://localhost/hopeImages/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:[[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=\".png\"\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 the body of the post to the reqeust
    [request setHTTPBody:body];

}

Sorry for all the unused code, I have been trying several solutions, none of which have worked of course...

Here is the PHP file, upload.php

<?php
$uploaddir = './';      //Uploading to same directory as PHP file
$file = basename($_FILES['userfile']['name']);
$uploadFile = $file;
$randomNumber = rand(0, 99999); 
$newName = $uploadDir . $randomNumber . $uploadFile;


if (is_uploaded_file($_FILES['userfile']['tmp_name'])) {
    echo "Temp file uploaded. \r\n";
} else {
    echo "Temp file not uploaded. \r\n";
}

if ($_FILES['userfile']['size']> 300000) {
    exit("Your file is too large."); 
}
?>

As for HTML, I have no idea where I would put it. Do I even use HTML in this situation? Do I run the PHP before the XCode application? Any answers are greatly appreciated.

halfer
  • 19,824
  • 17
  • 99
  • 186
  • also my upload_files is set to on – cmanquchmin Mar 23 '15 at 19:57
  • Create a simple HTML form in a page that posts to the address of your PHP script, with `userfile` as the name of an upload control. Add a submit button, and see if you get the same error. You can use this as a test-bed too - it removes the uncertainty of the problem being in your Objective C code. – halfer Mar 23 '15 at 20:34

0 Answers0