2012-08-17 98 views
2

我正嘗試在遠程服務器上傳圖像。如果我從谷歌的手機上下載圖片並上傳,例如http://www.mangauk.com/gallery/albums/album-11/lg/scooby.jpeg,它就可以上傳。但是,如果我嘗試上載使用相機拍攝的圖像,則不起作用。服務器掛起。從iOS設備上傳圖像到服務器

(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}"; 
} 
+3

如果有人能夠幫助您而無法查看您的代碼,該如何處理? – vikingosegundo 2012-08-17 10:34:28

+0

你可以把代碼放在這裏嗎? – 2012-08-17 10:34:33

+0

使用你的感官,@vikingosegundo,使用你的額外感官...)))沒有人閱讀常見問題前張貼問題... – 2012-08-17 10:38:33

回答

3

的問題必須在你的PHP腳本,因爲該文件必須得到發送給它。

你應該在你的php中定義文件的名字,而不是相信原始文件名。

否則有人可能會上傳一個惡意的.php文件,並且您的腳本會很高興地上傳它並將其命名爲.php,並且它們可以接管您的服務器。

你應該做這樣的事情:

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

這很可能也解決了JPEG/JPG問題becasue你正在做的命名。

另一件事 - 你也在做一個同步請求 - 這意味着你的應用程序只會在請求時等待並凍結(如果它是一個大文件,這可能是幾秒鐘),看看做一個異步請求。這意味着它會在後臺上傳圖片,然後在完成後調用一個函數,以便您可以告訴用戶所有上傳的確定。