php - Saving image on db mysql through swift

348

i have a problem with my php file. I'm trying to upload an image through xcode to mysql db.

here's swift code

let image = UIImage(named: "image.jpg")

    var imageData = UIImageJPEGRepresentation(image, 1)

    if imageData != nil{
        var request = NSMutableURLRequest(URL: NSURL(string:"link php file")!)
        var session = NSURLSession.sharedSession()

        request.HTTPMethod = "POST"

        var boundary = NSString(format: "

and here's php code:

{-code-2}

the problem is in the path. it returns wrong path and so i found no image in "uploads" folder. if i change target_path in php file (for example $target_path = "uploads/image.jpg"), image is saved.

i think it's a very easy problem but i don't understand what i have to change.

thanks for any help

hi

118

Answer

-------14737809831466499882746641449") var contentType = NSString(format: "multipart/form-data; boundary=%@",boundary) request.addValue(contentType, forHTTPHeaderField: "Content-Type") var body = NSMutableData.alloc() // Image body.appendData(NSString(format: "--%@\r\n", boundary).dataUsingEncoding(NSUTF8StringEncoding)!) body.appendData(NSString(format:"Content-Disposition: attachment; name=\"userfile\"; filename=\"img.jpg\"").dataUsingEncoding(NSUTF8StringEncoding)!) //body.appendData(NSString(format: "Content-Type: application/octet-stream\r\n\r\n").dataUsingEncoding(NSUTF8StringEncoding)!) body.appendData(imageData) body.appendData(NSString(format: "\r\n--%@--\r\n", boundary).dataUsingEncoding(NSUTF8StringEncoding)!) request.HTTPBody = body var returnData = NSURLConnection.sendSynchronousRequest(request, returningResponse: nil, error: nil) var returnString = NSString(data: returnData!, encoding: NSUTF8StringEncoding) println("returnString \(returnString)") }|||$myparam = $_FILES['userfile']; //getting image Here //getting textLabe Here $target_path = "uploads/".basename($myparam['filename']); if(move_uploaded_file($myparam['tmp_name'], $target_path)){ $var = 1; } else { $var = 0; } $myarray = array("response" => $var, "path" => $target_path); $out = json_encode($myarray); echo($out);

People are also looking for solutions to the problem: php - Facebook SDK V4 get name

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.