PHP upload from iOS using Alamofire PHP upload from iOS using Alamofire swift swift

PHP upload from iOS using Alamofire


Problem is in your savePhotos function of PHP source. Also some extra code written in iOS code. I have removed extra code. Try once.

Here is the iOS code updated

Alamofire.upload(.POST, urlString, multipartFormData: {        multipartFormData in        if (firstPhoto != nil) {            if  let firstImageData = UIImagePNGRepresentation(firstImage!, 0.6) {                print("uploading image");                multipartFormData.appendBodyPart(data: firstImageData, name: "firstImage", fileName: "firstImage.png", mimeType: "image/png")            }        }        if (secondPhoto != nil) {            if  let secondImageData = UIImagePNGRepresentation(secondImage!, 0.6) {                print("uploading image");                multipartFormData.appendBodyPart(data: secondImageData, name: "secondImage", fileName: "secondImage.png", mimeType: "image/png")            }        }        if (thirdPhoto != nil) {            if  let thirdImageData = UIImagePNGRepresentation(thirdImage!, 0.6) {                print("uploading image");                multipartFormData.appendBodyPart(data: thirdImageData, name: "thirdImage", fileName: "thirdImage.png", mimeType: "image/png")            }        }        for (key, value) in parameters {            multipartFormData.appendBodyPart(data: value.dataUsingEncoding(NSUTF8StringEncoding)!, name: key)        }        }, encodingCompletion: {            encodingResult in            switch encodingResult {            case .Success(let upload, _, _):                print("Запрос отправлен")                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                    if let JSON = response.result.value {                        print("JSON: \(JSON)")                    }                    completion(response.result.value)                }            case .Failure(let encodingError):                print(encodingError)                completion(nil)            }    })}

And here is the PHP code

function savePhotos($mysqli, $bookID) {    $basePath = '/home/thebakpa/resources/book_photos/'    if(!is_dir($basePath)) {        mkdir($basePath, 0777, true);    }    try {        $file1 = $_FILES['firstImage'];        if (is_uploaded_file($file1['tmp_name'])) {            $photoPath = $basePath.'photo1-'.$bookID.'.jpg';            if (move_uploaded_file($file1['tmp_name'], $photoPath)) {                $mysqli->query('UPDATE Book SET photo1='.$photoPath.' WHERE bookID='.$bookID.'');            }        }        $file2 = $_FILES['secondImage'];        if (is_uploaded_file($file2['tmp_name'])) {            $photoPath = $basePath.'photo2-'.$bookID.'.jpg';            if (move_uploaded_file($file2['tmp_name'], $photoPath)) {                $mysqli->query('UPDATE Book SET photo2='.$photoPath.' WHERE bookID='.$bookID.'');            }        }        $file3 = $_FILES['thirdImage'];        if (is_uploaded_file($file3['tmp_name'])) {            $photoPath = $basePath.'photo3-'.$bookID.'.jpg';            if (move_uploaded_file($file3['tmp_name'], $photoPath)) {                $mysqli->query('UPDATE Book SET photo3='.$photoPath.' WHERE bookID='.$bookID.'');            }        }    } catch(Exception $ex){        echo "ERROR:".$ex->GetMessage()."\n";        exit(1);    }}


The names do not match ['userfile']['photo1'] in your php. But on the iOS side userfile and photo1 are never used. Try using $_FILES['firstImage']


try this if you submit image in json format(repeat same code for other images by chnaging image name)

<?php                   $image          = $file;            $directorypath1 = $uploaddir;            $img            = str_replace('data:image/PNG;base64,', '', $image);            $img            = str_replace(' ', '+', $img);            $data           = base64_decode($img);            $name           = rand() . '_' . time() . ".jpg";            $img_path       = $directorypath1 . $name;            file_put_contents($img_path, $data);?>