user3592917 user3592917 - 4 months ago 257
Swift Question

Swift 3 Upload image to PHP Fail

Upload image fail
I send image by POST to php

enter image description here

enter image description here

class ViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {

@IBOutlet var image: UIImageView!

override func viewDidLoad() {

@IBAction func selectPicture(_ sender: AnyObject) {

let ImagePicker = UIImagePickerController()
ImagePicker.delegate = self
ImagePicker.sourceType = UIImagePickerControllerSourceType.photoLibrary

self.present(ImagePicker, animated: true, completion: nil)

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {

image.image = info[UIImagePickerControllerOriginalImage] as? UIImage
self.dismiss(animated: true, completion: nil)

@IBAction func upload_request(_ sender: AnyObject) {

func UploadRequest()
let url = URL(string: "")

let request = NSMutableURLRequest(url: url!)
request.httpMethod = "POST"

let boundary = generateBoundaryString()

request.setValue("multipart/form-data; boundary=\(boundary)", forHTTPHeaderField: "Content-Type")

if (image.image == nil)

let image_data = UIImagePNGRepresentation(image.image!)

if(image_data == nil)

let body = NSMutableData()

let fname = "test.png"
let mimetype = "image/png"

body.append("--\(boundary)\r\n".data(using: String.Encoding.utf8)!)
body.append("Content-Disposition:form-data; name=\"test\"\r\n\r\n".data(using: String.Encoding.utf8)!)
body.append("hi\r\n".data(using: String.Encoding.utf8)!)

body.append("--\(boundary)\r\n".data(using: String.Encoding.utf8)!)
body.append("Content-Disposition:form-data; name=\"file\"; filename=\"\(fname)\"\r\n".data(using: String.Encoding.utf8)!)
body.append("Content-Type: \(mimetype)\r\n\r\n".data(using: String.Encoding.utf8)!)
body.append("\r\n".data(using: String.Encoding.utf8)!)

body.append("--\(boundary)--\r\n".data(using: String.Encoding.utf8)!)

request.httpBody = body as Data
let session = URLSession.shared

let task = URLSession.shared.dataTask(with: request as URLRequest) { (
data, response, error) in

guard let _:Data = data, let _:URLResponse = response , error == nil else {

let dataString = NSString(data: data!, encoding: String.Encoding.utf8.rawValue)


func generateBoundaryString() -> String
return "Boundary-\(UUID().uuidString)"


if (move_uploaded_file($_FILES['file']['tmp_name'], "image.png")) {
echo "File uploaded: ".$_FILES["file"]["name"]; }


Your second screenshot shows the problem: this is the server reply with a PHP warning, the apache/php user does not have the permissions to move the uploaded image to the targetdirectory in your case this would be the root directory (/) - because you did not specify a directory/path where to store the image. See Example 1 of the PHP documentation.

So you should create an upload directory (e.g. mkdir /Appli.../htdocs/uploads , set the correct permissions (for dev purpose chmod -R 777 uploads) and specify in your PHP the target directory:

if (move_uploaded_file($_FILES['file']['tmp_name'], "/Appli.../htdocs/uploads/image.png")) { ...