简体   繁体   中英

Swift 3 Upload image to PHP Fail

Upload image fail I send image by POST to php

在此处输入图片说明

在此处输入图片说明

class ViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {

@IBOutlet var image: UIImageView!

override func viewDidLoad() {
    super.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) {
    UploadRequest()
}   

func UploadRequest()
{
    let url = URL(string: "http://127.0.0.1/imgJSON/img.php")

    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)
    {
        return
    }

    let image_data = UIImagePNGRepresentation(image.image!)

    if(image_data == nil)
    {
        return
    }

    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(image_data!)
    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 {
            print("error")
            return
        }

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

        print(dataString)            
    }
    task.resume()       
}    

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

In PHP

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 (eg 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")) { ...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM