1

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: "---------------------------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)")

    }

and here's php code:

$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);

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

2
  • var_dump($_FILES) will show you exactly where you're going wrong: it's ['name'], not ['filename']. And your code is simply assuming nothing could ever go wrong and that uploads will never fail. That is a very BAD assumption. Commented Feb 3, 2015 at 21:01
  • Thanks for the answer. I know i'm assuming that up never fail but it's just a test; of course it has to be more complicated than that. Anyway I don't understand your advice. What have i to change in my code? php or swift? and where? thanks Commented Feb 3, 2015 at 21:14

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.