2014-08-31 176 views
0

我想從iOS上傳圖片和標題到我的服務器。我有一個成功接收圖像和標題的PHP,但後來發現錯誤:上傳文件時出現錯誤,請重試。什麼是錯誤的PHP?php上傳圖片失敗上傳

<?php 
$myparam = $_POST['userfile'];  //getting image Here 
$mytextLabel= $_POST['filenames']; //getting textLabe Here 
echo $myparam; 
echo $mytextLabel; 
$target_path = "upload/"; 
$target_path = $target_path . basename($_FILES['myfile']['name']); 

if(move_uploaded_file($_FILES['myfile']['tmp_name'], $target_path)) { 
    echo "The file ". basename($_FILES['myfile']['name']) . " has been uploaded"; 
} else { 
    echo "There was an error uploading the file, please try again!"; 
} 
?> 

編輯:下面是iOS的端代碼,但大量的工作後,所以工作

NSString *filenames = @"title name";  //set name here 
     NSLog(@"%@", filenames); 
     NSString *urlString = @"http://www.myserver.com/uploads.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:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"filenames\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]]; 
     [body appendData:[filenames dataUsingEncoding:NSUTF8StringEncoding]]; 

     [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:[@"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]]; 
     // 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]; 
+1

向我們展示你的HTML表單。 – 2014-08-31 18:08:21

+0

沒有html ...它直接從iOS上傳到此。我會張貼。 – 2014-08-31 18:09:08

+0

如果您的文件輸入是「userfile」,那麼您必須使用$ _FILES ['userfile'] ['tmp_name']'。還要確保你的表單'enctype = multipart/form-data' – jboneca 2014-08-31 18:10:41

回答

0

(約48個小時了),我設法得到它的工作。 的iOS:

NSData *imageData =UIImageJPEGRepresentation(imageView.image, 90); 
    NSString *imgPath= @"http://www.myserver.com/uploads.php"; 
     NSString *filenames = labelb.text; //name of picture excluding .jpg 

     NSString *urlString = imgPath; 
     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:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"filenames\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]]; 
     [body appendData:[filenames dataUsingEncoding:NSUTF8StringEncoding]]; 

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

     // [body appendData:[@"Content-Disposition: form-data; name=\"userfile\"; filename=\".jpg\"\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]]; 
     // 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]; 
     if([returnString isEqualToString:@"yes"]) 
     { 
      [self finitop]; 

     } 
     else { 
      [spinner stopAnimating]; 
      label.text = @"Posted Empty Because Upload Failed"; 

     } 

PHP:

<?php 
$uploaddir = './upload/'; 
$file = basename($_FILES['userfile']['name']); 
$uploadfile = $uploaddir . $file; 

if (is_uploaded_file($_FILES['userfile']['tmp_name'])) 
{ 
if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) 
{ 
    echo "yes"; 
} 

} 
else 
{ 
    echo "NO "; 
} 


?>