2017-01-25 2 views
1

내 서버에 Alamofire와 PHP를 사용하여 이미지를 업로드하려고하는데, 여기에 다른 질문을 모두 체크했으나 작동하지 않습니다. PHP 파일을 HTML로 테스트 한 결과 이미지를 업로드하는 데 성공 했으므로 신속한 코드로 축소되었습니다.Alamofire Swift 3 + PHP 업로드 이미지가 작동하지 않습니다.

스위프트 :

let URL = "http://example.com/post_pic.php" 
    let imageData = UIImageJPEGRepresentation(imageView.image!, 1) 


    Alamofire.upload(multipartFormData: { (multipartFormData) in 
     multipartFormData.append(imageData!, withName: "imageFile", mimeType: "image/jpeg") 
    }, to:URL) 
    { (result) in 
     switch result { 
     case .success(let upload, _, _): 

      upload.uploadProgress(closure: { (Progress) in 
       print("Upload Progress: \(Progress.fractionCompleted)") 
      }) 

      upload.responseJSON { response in 
       print(response.request) // original URL request 
       print(response.response) // URL response 
       print(response.data)  // server data 
       print(response.result) // result of response serialization 
       //      self.showSuccesAlert() 
       //self.removeImage("frame", fileExtension: "txt") 
       if let JSON = response.result.value { 
        print("JSON: \(JSON)") 
       } 

      } 

     case .failure(let encodingError): 
      print("Failure:") 
      print(encodingError) 
     } 

    } 

PHP :

if(isset($_FILES['imageFile'])){ 
    $errors= array(); 
    $file_name = $_FILES['imageFile']['name']; 
    $file_size =$_FILES['imageFile']['size']; 
    $file_tmp =$_FILES['imageFile']['tmp_name']; 
    $file_type=$_FILES['imageFile']['type']; 
    $file_ext=strtolower(end(explode('.',$_FILES['imageFile']['name']))); 
    print_r($_FILES['imageFile']); 

    if(empty($errors)==true){ 
     move_uploaded_file($file_tmp,"images/".$file_name); 
    } 
    else 
    { 
     echo '{"response":"file_move_error"}'; 
    } 
} 
else 
{ 
     echo '{"response":"file_error"}'; 

} 

내가 얻을 응답은 다음과 같습니다

Optional(<NSHTTPURLResponse: 0x17022df20> { URL: http://example.com/post_pic.php } { 
     status code: 200, headers { 
    Connection = "Keep-Alive"; 
    "Content-Length" = 25; 
    "Content-Type" = "application/json"; 
    Date = "Wed, 25 Jan 2017 10:16:29 GMT"; 
    "Keep-Alive" = "timeout=5, max=100"; 
    Server = "Apache/2.4.7 (Ubuntu)"; 
    "X-Powered-By" = "PHP/5.5.9-1ubuntu4.20"; 
} }) 
     Optional(25 bytes) 
     SUCCESS 
     JSON: { 
      response = "file_error"; 
     } 

답변

0

나는 내 응용 프로그램에이 코드를 사용할 수 있습니다

let URL = "http://example.com/post_pic.php" 
let imageData = UIImageJPEGRepresentation(imageView.image!, 1) 


    upload(multipartFormData: { multipartFormData in 
        multipartFormData.append(imageData!, withName: "imageFile", fileName: "file.jpg", mimeType: "image/jpeg") 
     }, 
      to: URL, method: .post, 
      encodingCompletion: { encodingResult in 

       switch encodingResult { 
       case .success(let upload, _, _): 


        upload.validate() 
        upload.responseJSON { response in 


         if response.result.error != nil { 
          print("failure") 
          UIAlertView(title: "Fail", message: "Please retry again.", delegate: nil, cancelButtonTitle: "Ok").show() 
         } else { 
          print("success") 


         } 
        } 
       case .failure(let encodingError): 
        print(encodingError) 

       } 
     } 
     ) 
+0

여전히 작동하지 않습니다, – MasterWizard

+0

'multipartFormData.append (imageData !, withName : "imageFile", fileName = xxxx.jpg, mimeType : "image/jpeg") – Ronak

+0

: multipartFormData.append (imageData !, withName : "imageFile", fileName : "xxxx.jpg", mimeType : "image/jpeg) – MasterWizard

관련 문제