In my swift app I've the following function:
func imageUploadRequest(imageView: UIImageView, uploadUrl: NSURL, param: [String:String]?) { let request = NSMutableURLRequest(url:uploadURL! as URL); request.httpMethod = "POST" let postParameters = "email=" + String(email) request.httpBody = postParameters.data(using: String.Encoding.utf8) let boundary = generateBoundaryString() request.setValue("multipart/form-data; boundary=\(boundary)", forHTTPHeaderField: "Content-Type") let imageData = UIImageJPEGRepresentation(imageView1.image!, 1.0) if (imageData == nil) { return } request.httpBody = createBodyWithParameters(parameters: param, filePathKey: "file", imageDataKey: imageData! as NSData, boundary: boundary) as Data let task = URLSession.shared.dataTask(with: request as URLRequest, completionHandler: { (data, response, error) -> Void in if let data = data { _ = try!JSONSerialization.jsonObject(with: data, options: .mutableContainers) as? NSDictionary } else if let error = error { print(error.localizedDescription) } }) task.resume() } func createBodyWithParameters(parameters: [String: String]?, filePathKey: String?, imageDataKey: NSData, boundary: String) -> NSData { let body = NSMutableData();
if parameters != nil { for (key, value) in parameters! { body.appendString(string: "--\(boundary)\r\n") body.appendString(string: "Content-Disposition: form-data; name=\"\(key)\"\r\n\r\n") body.appendString(string: "\(value)\r\n") } } let filename = profilePicturePath let mimetype = "image/jpg" body.appendString(string: "--\(boundary)\r\n") body.appendString(string: "Content-Disposition: form-data; name=\"\(filePathKey!)\"; filename=\"\(filename)\"\r\n") body.appendString(string: "Content-Type: \(mimetype)\r\n\r\n") body.append(imageDataKey as Data) body.appendString(string: "\r\n") body.appendString(string: "--\(boundary)--\r\n") return body } func generateBoundaryString() -> String { return "Boundary-\(NSUUID().uuidString)" } func downloadImage(url: URL) { getDataFromUrl(url: url) { data, response, error in guard let data = data, error == nil else { return } print(response?.suggestedFilename ?? url.lastPathComponent) DispatchQueue.main.async() { self.imageView1.image = UIImage(data: data) self.tableView.reloadData() } } } USER_IMG_SET.php
$email = $_POST['email']; //$email = "[email protected]"; $target_dir = "../usersAccountData/".$email; if(!file_exists($target_dir)) { mkdir($target_dir, 0755, true); } $target_dir = $target_dir . "/" . basename($_FILES["file"]["name"]); if (move_uploaded_file($_FILES["file"]["tmp_name"], $target_dir)) { echo json_encode([ "Message" => "The file ". basename( $_FILES["file"]["name"]). " has been uploaded.", "Status" => "OK", "userId" => $_REQUEST["userId"] ]); } else { echo json_encode([ "Message" => "Sorry, there was an error uploading your file.", "Status" => "Error", "userId" => $_REQUEST["userId"] ]); } I uploaded image data and I also want to upload the user email in php ($email = $_POST['email'];) with the same request. How can I do it?
Because I don't have other details to write! « Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam eaque ipsa, quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt, explicabo. Nemo enim ipsam voluptatem, quia voluptas sit, aspernatur aut odit aut fugit, sed quia consequuntur magni dolores eos, qui ratione voluptatem sequi nesciunt, neque porro quisquam est, qui dolorem ipsum, quia dolor sit, amet, consectetur, adipisci velit, sed quia non numquam eius modi tempora incidunt, ut labore et dolore magnam aliquam quaerat voluptatem. »
createBodyWithParameters(parameters:filePathKey:imageDataKey:boundary:). Also, you are not showing how you are calling yourimageUploadRequest(imageView:uploadUrl:param:). You may need to pass["email": email]toparam:, when calling the method.