简体   繁体   中英

PHP picture upload works on localhost but not on GoDaddy server

We have been developing an application for both iOS and Android. One of our views uploads a picture to the server using POST. The android version works both in our local server and in the website. However, the iOS version is only working on our local server at the moment. Here is the code used to write the POST request:

NSData* imageData = UIImageJPEGRepresentation(image, 1.0);

    // Create the request.
    NSString* afterURL = [NSString stringWithFormat:@"upload.php?username=%@",username];
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc]
                                    initWithURL:[NSURL URLWithString:
                                                 [NSString stringWithFormat:@"%@%@",url,afterURL]]];
    [request setHTTPMethod:@"POST"];

    //------------------------------------------------------
    // used code from this tutorial: http://www.iriphon.com/2011/11/09/ios-uploading-an-image-from-your-iphone-to-a-server/
    NSString *boundary = @"-----InstantLabor";
    NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
    [request addValue:contentType forHTTPHeaderField: @"Content-Type"];

    NSMutableData *body = [NSMutableData data];

    // Now we need to append the different data 'segments'. We first start by adding the boundary.
    [body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];

    // This line of code came from http://stackoverflow.com/questions/20728317/upload-image-to-the-php-server-from-ios
    //   By creating the string this way instead of the other way, we were able to successfully upload the picture!
    /*
    [body appendData:[[NSString stringWithString:
                       [NSString stringWithFormat:@"Content-Disposition: form-data; name=\"userfile\"; filename=\"%@.jpg\"\r\n", @"dl"]]
                      dataUsingEncoding:NSUTF8StringEncoding]];
     */

     [body appendData:[@"Content-Disposition: form-data; name=\"attachment[userfile]\";filename=\"dl.jpg\"\r\n"
                      dataUsingEncoding:NSUTF8StringEncoding]];


    // We now need to tell the receiver what content type we have
    // In my case it's a png image. If you have a jpg, set it to 'image/jpg'
    [body appendData:[@"Content-Type: image/jpg\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];

    // Now we append the actual image data
    [body appendData:[NSData dataWithData:imageData]];

    // and again the delimiting boundary
    [body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];

    // adding the body we've created to the request
    [request setHTTPBody:body];
    //-----------------------------------------------------

    // Create url connection and fire request
    _parsedData = nil;
    [NSURLConnection connectionWithRequest:request delegate:self];

When we were trying to get it to work in the local server, the above code didn't work until we switched

[body appendData:[@"Content-Disposition: form-data; name=\"attachment[userfile]\";filename=\"dl.jpg\"\r\n"
                  dataUsingEncoding:NSUTF8StringEncoding]];

with

[body appendData:[[NSString stringWithString:
                   [NSString stringWithFormat:@"Content-Disposition: form-data; name=\"userfile\"; filename=\"%@.jpg\"\r\n", @"dl"]]
                  dataUsingEncoding:NSUTF8StringEncoding]];

Even though it is creating the same string, one works and other one doesn't; but neither of them work when connecting to the website. Everything we've tried ends up spitting out

Undefined index: userfile

back at the application. Since one of them works, we know it's not a memory issue or a privileges issue. We have no idea what else could be causing the problem.

Thanks in advance for your help.

After two days of tweaking, testing. etc we had no luck solving the issue. Instead we decided to incorporate AFNetworking and follow this Upload image from iOS to PHP to get our picture upload to work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM