简体   繁体   中英

Picture upload to PHP server doesn't work on a real iOS device

I have a problem to upload some pictures on my web server from the iPhone/ipad. Indeed, my code works really well on the simulator iPhone on the MAC but on a real device, it doesn't work, pictures are not uploaded on my server. I don't know where is the problem and why it works on the simulator and not on my device. I have this code in objc and i call a php file to save image to the server.

the objective c code :

NSData *storeData = UIImageJPEGRepresentation(pictureItem, 90); 
NSString *URLString = @"http://url_example/upload.php"; //call 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=\"userfile\"; filename=\"%d.jpg\"\r\n", idItem] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[NSData dataWithData:storeData]];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[request setHTTPBody:body];

NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
NSLog(@"%@",returnString);

The php file is here :

<?php
    $uploaddir = './';      //Uploading to same directory as PHP file
    $file = basename($_FILES['userfile']['name']);
    $uploadFile = $file;
    $newName = $uploadDir . $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']> 10000000) {
        exit("Your file is too large."); 
    }

    if (move_uploaded_file($_FILES['userfile']['tmp_name'], $newName)) {
        $postsize = ini_get('post_max_size');   //Not necessary, I was using these
        $canupload = ini_get('file_uploads');    //server variables to see what was 
        $tempdir = ini_get('upload_tmp_dir');   //going wrong.
        $maxsize = ini_get('upload_max_filesize');
    }
?>

Thank you for your help!

Double check the case of your image filename. The simulator usually will forgive a file-case discrepancy, but a device usually won't.

For example, if you have a file in your project called Cat.png , and in your code you reference the resource as cat.png (lower case 'c'), it often will work in the simulator but not on a device.

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