从iOS设备上传图片到服务器 [英] Upload image to server from iOS device

查看:228
本文介绍了从iOS设备上传图片到服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试在远程服务器上上传图片。如果我从Google下载我手机上的图片并上传,例如 http: //www.mangauk.com/gallery/albums/album-11/lg/scooby.jpeg 上传正常。但是,如果我尝试上传图像,我用相机拍摄,它不工作。服务器刚挂起。

I'm trying to upload an image on a remote server. If i download images on my phone from google and upload them e.g http://www.mangauk.com/gallery/albums/album-11/lg/scooby.jpeg it uploads just fine. However if i try to upload images that i have taken with the camera, it does not work. The server just hangs.

(IBAction)uploadImage {
    /*
     turning the image into a NSData object
     getting the image back out of the UIImageView
     setting the quality to 90
    */
    NSData *imageData = UIImageJPEGRepresentation(image.image, 90);
    // setting up the URL to post to
    NSString *urlString = @"http://iphone.zcentric.com/test-upload.php";

    // setting up the request object now
    NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
    [request setURL:[NSURL URLWithString:urlString]];
    [request setHTTPMethod:@"POST"];

    /*
     add some header info now
     we always need a boundary when we post a file
     also we need to set the content type

     You might want to generate a random boundary.. this is just the same 
     as my output from wireshark on a valid html post
    */
    NSString *boundary = [NSString stringWithString:@"---------------------------14737809831466499882746641449"];
    NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
    [request addValue:contentType forHTTPHeaderField: @"Content-Type"];

    /*
     now lets create the body of the post
    */
    NSMutableData *body = [NSMutableData data];
    [body appendData:[[NSString stringWithFormat:@"rn--%@rn",boundary] dataUsingEncoding:NSUTF8StringEncoding]];    
    [body appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name="userfile"; filename="ipodfile.jpg"rn"] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[[NSString stringWithString:@"Content-Type: application/octet-streamrnrn"] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[NSData dataWithData:imageData]];
    [body appendData:[[NSString stringWithFormat:@"rn--%@--rn",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
    // setting the body of the post to the reqeust
    [request setHTTPBody:body];

    // now lets make the connection to the web
    NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
    NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];

    NSLog(returnString);
}

和php脚本

$uploaddir = './uploads/';
$file = basename($_FILES['userfile']['name']);
$uploadfile = $uploaddir . $file;

if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
        echo "http://iphone.zcentric.com/uploads/{$file}";
}


推荐答案

问题必须在因为文件必须发送给它。

The problem must be in your php script since the file must be getting sent to it.

您应该在您的php中定义文件的名称,而不是信任原始文件名。

You should be defining the name of the file in your php, rather than trusting the original file name.

否则有人可能会上传一个恶意的.php文件,您的脚本会很高兴地将其上传并命名为.php,他们可以接管您的服务器。

Otherwise someone could upload a malicious .php file and your script would happily upload it and name it .php and they could take over your server.

你应该这样做:

$uploaddir = './uploads/';
$file = $uploaddir . 'filename_chosen_by_you.jpg';

这可能也会解决jpeg / jpg问题,因为你在做命名。

This will likely also solve the jpeg / jpg problem becasue you're doing the naming.

另一件事 - 你也在做一个同步请求 - 意味着你的应用程序只会等待和冻结,而执行请求(如果它的一个大文件可能是几秒钟) ,看看做一个异步请求。这意味着它将在后台上传图片,然后在完成后调用一个函数,以便您可以告诉用户一切已上传完成。

One other thing- you're also doing a synchronous request- meaning your app will just wait and freeze while doing the request (which could be a few seconds if its a big file), have a look at doing an asynchronous request. That means it will upload the image in the background and then call a function when finished, so you can tell the user everything's uploaded OK.

这篇关于从iOS设备上传图片到服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆