2013-12-23 82 views
0

我遇到問題。我一直在看這裏的其他答案,但無法得到解決方案。我的代碼似乎工作,(日誌給我我的形象已經上傳到服務器),但是當我打開服務器沒有得到任何東西。你能幫我?我要瘋了感謝我在iOS和PHP上傳圖片的代碼不起作用

這裏是我的IOS代碼.M:

- (IBAction)pushUpload:(id)sender { 
    NSData *imageData =UIImageJPEGRepresentation(imageView.image, 90); 
    NSString *urlString= @"http://www.myserver/test/upload.php"; 
    NSMutableURLRequest *request =[[[NSMutableURLRequest alloc]init]autorelease]; 
    [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:[@"Content-Disposition: form-data; name=\"userfile\";filename=\".jpg\"\r\n" 
         dataUsingEncoding:NSUTF8StringEncoding]]; 

    [body appendData:[[NSString stringWithFormat:@"Content-Type: application/octet-stream\r\n\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]] ; 

    [request setHTTPBody:body]; 

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

,這是我的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']> 300000000000) { 
    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'); 
    echo "http://www.myweb.com/dir/{$file}" . "\r\n" . $_FILES['userfile']['size'] . "\r\n" . $_FILES['userfile']['type'] ; 
} 
?> 
+0

你爲此做了哪些調試?你的'move_uploaded_file()'返回true嗎? – andrewsi

+0

該文件是否具有對上傳目錄的寫入權限? – AustinAllover

回答

0

好吧,這是一個愚蠢的權限問題服務器文件夾。 謝謝@AustinAllover!