Specific php and swift code for image uploading to sftp -


php script , xcode shows here

****** response data =
notice: undefined index: userid in /home/raimonds/script.php on line 36
{"message":"first name field empty","status":"error","userid":null}

so here php code :

<?php if(!empty($_post["firstname"])){      $firstname = isset($_post["firstname"]) ? $_post["firstname"] : "";     $lastname = isset($_post["lastname"]) ? $_post["lastname"] : "";     $userid = isset($_post["userid"]) ? $_post["userid"] : "";     $target_dir = "uploads/";      if(!file_exists($target_dir))     {     mkdir($target_dir, 0777, true);     }      $target_dir = $target_dir . "/" . basename($_files["file"]["name"]);      if (move_uploaded_file($_files["file"]["tmp_name"], $target_dir)) {         echo json_encode([         "message" => "the file ". basename( $_files["file"]["name"]). " has been uploaded.",         "status" => "ok",         "userid" => $_post["userid"]         ]);      } else {         echo json_encode([         "message" => "sorry, there error uploading file.",         "status" => "error",         "userid" => $_post["userid"]         ]);     }  }else{          echo json_encode([         "message" => "first name field empty",         "status" => "error",         "userid" => $_post["userid"]         ]);  }  ?> 

can please me? xcode part was:

// //  viewcontroller.swift //  imageuploader // //  created raimonds on 14/05/15. //  copyright (c) 2015 raimonds. rights reserved. //  import uikit  class viewcontroller: uiviewcontroller, uiimagepickercontrollerdelegate,     uinavigationcontrollerdelegate  {     @iboutlet weak var myimageview: uiimageview!      @iboutlet weak var myactivityindicator: uiactivityindicatorview!      override func viewdidload() {         super.viewdidload()         // additional setup after loading view, typically nib.     }      override func didreceivememorywarning() {         super.didreceivememorywarning()         // dispose of resources can recreated.     }       @ibaction func uploadbuttontapped(sender: anyobject) {          myimageuploadrequest()      }      @ibaction func selectphotobuttontapped(sender: anyobject) {          var mypickercontroller = uiimagepickercontroller()         mypickercontroller.delegate = self;         mypickercontroller.sourcetype = uiimagepickercontrollersourcetype.photolibrary          self.presentviewcontroller(mypickercontroller, animated: true, completion: nil)      }      func imagepickercontroller(picker: uiimagepickercontroller, didfinishpickingmediawithinfo info: [nsobject : anyobject])      {       myimageview.image = info[uiimagepickercontrolleroriginalimage] as?         uiimage          self.dismissviewcontrolleranimated(true, completion: nil)      }          func myimageuploadrequest()      {          let myurl = nsurl(string: "http://raimonds.gtd.lv/script.php");         let request = nsmutableurlrequest(url: myurl!);         request.httpmethod = "post";           let param = [             "firstname"  :  "firstname",             "lastname"  :  "lastname",             "userid"  :  "9"         ]          let boundary = generateboundarystring()          request.setvalue("multipart/form-data: boundary=\(boundary)" , forhttpheaderfield: "content-type")          let imagedata = uiimagejpegrepresentation(myimageview.image, 1)          if (imagedata==nil) {return; }          request.httpbody = createbodywithparameters(param, filepathkey: "file", imagedatakey: imagedata, boundary: boundary)          myactivityindicator.startanimating();          let task = nsurlsession.sharedsession().datataskwithrequest(request) {             data, response, error in              if error != nil {                 println("error=\(error)")                 return             }               println("******* response = \(response)")              let responsestring = nsstring(data: data, encoding: nsutf8stringencoding)             println("****** response data = \(responsestring!)")              var err: nserror?             var json = nsjsonserialization.jsonobjectwithdata(data, options: .mutablecontainers, error: &err)               as? nsdictionary               dispatch_async(dispatch_get_main_queue(),{                 self.myactivityindicator.stopanimating()                 self.myimageview.image = nil;              });       }       task.resume()   }       func createbodywithparameters(parameters: [string: string]?, filepathkey: string?, imagedatakey: nsdata, boundary: string) -> nsdata  {          var body = nsmutabledata();          if parameters != nil {             (key, value) in parameters! {                 body.appendstring("--\(boundary)\r\n")                 body.appendstring("content-disposition: form-data; name=\"\(key)\"\r\n\r\n")              }         }          let filename = "user-profile.jpg"          let mimetype = "image/jpg"          body.appendstring("--\(boundary)\r\n")         body.appendstring("content-disposition: form-data; name=\"\(filepathkey!)\"; filename=\"\(filename)\"\r\n")         body.appendstring("content-type: \(mimetype)\r\n\r\n")         body.appenddata(imagedatakey)         body.appendstring("\r\n")          body.appendstring("--\(boundary)--\r\n")          return body     }      func generateboundarystring() -> string {         return "boundary--\(nsuuid().uuidstring)"     }  }  extension nsmutabledata {     func appendstring(string: string) {         let data = string.datausingencoding(nsutf8stringencoding, allowlossyconversion: true)         appenddata(data!)     }     } 

you have problem in swift code, because don't send post data php

try how send swift post request: http://ios-blog.co.uk/tutorials/swift-how-to-send-a-post-request-to-a-php-script/

you need that:

var url: nsurl = nsurl(string: "http://raimonds.gtd.lv/script.php")! var request:nsmutableurlrequest = nsmutableurlrequest(url:url) request.httpmethod = "post" var bodydata = "firstname=firstname&lastname=lastname&userid=9" request.httpbody = bodydata.datausingencoding(nsutf8stringencoding); nsurlconnection.sendasynchronousrequest(request, queue: nsoperationqueue.mainqueue()) {     (response, data, error) in     println(response)  } 

Comments

Popular posts from this blog

c++ - Difference between pre and post decrement in recursive function argument -

php - Nothing but 'run(); ' when browsing to my local project, how do I fix this? -

php - How can I echo out this array? -