Using Xcode 8, Swift 3 and PHP.
Xcode and PHP are running without error.
Why isn't displayPic.image from simulator saved to pic1-1.png at server? All that appears is a blank file.
Xcode:
#IBAction func sendToServer(_ sender: UIButton) {
let url = NSURL(string: "http://www.example.com/picSaver.php")
var request = URLRequest(url: url! as URL)
request.httpMethod = "POST"
var boundary = generateBoundaryString()
request.setValue("multipart/form-data; boundary=\(boundary)",
forHTTPHeaderField: "Content-Type")
if (displayPic.image == nil)
{ return }
let image_data = UIImagePNGRepresentation(displayPic.image!)
var body = NSMutableData()
let fname = "porch-167.png"
let mimetype = "image/png"
body.append("--\(boundary)\r\n".data(using: String.Encoding.utf8)!)
body.append("Content-Disposition:form-data;
name=\"photo\"\r\n\r\n".data(using: String.Encoding.utf8)!)
body.append("Incoming\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 = session.dataTask(with: request as URLRequest) {
(
data, response, error) in
guard let _:Data = data, let _:URLResponse = response , error
== nil else {
print("error")
return
}
let dataString = String(data: data!, encoding:
String.Encoding(rawValue: String.Encoding.utf8.rawValue))
print(dataString)
}
task.resume()
}
}
func generateBoundaryString() -> String {
return "Boundary-\(NSUUID().uuidString)"
}
picSaver.php
<?php
$p=$_REQUEST["filename"]
move_uploaded_file($p["tmp_name"], pic1-1.png);
$data=Array("Reply"=>"Image saved at server");
echo json_encode($data);
?>
Is "filename" the correct parameter request for $p $_REQUEST ?
It works flawlessly now. Change $_REQUEST to $_FILES, and "filename" to "file"
<?php
$p=$_FILES["file"]
move_uploaded_file($p["tmp_name"], "pic1-1.png");
$data=Array("Reply"=>"Image saved at server");
echo json_encode($data);
?>
Related
I'm trying to upload image to a local server using swift5 and php , Xcode Version 12.5.1 , Can you help me debug the code, or give me an example ?
Image upload example with Swift and PHP
I downloaded this example and change but it doesn't work, only folders created work
The modifications in the code :
import UIKit
class ViewController: UIViewController, UIImagePickerControllerDelegate,UINavigationControllerDelegate {
#IBOutlet weak var myActivityIndicator: UIActivityIndicatorView!
#IBOutlet weak var myImageView: UIImageView!
#IBAction func uploadButtonTapped(_ sender: Any) {
myImageUploadRequest()
}
#IBAction func selectPhotoButtonTapped(_ sender: Any) {
let myPickerController = UIImagePickerController()
myPickerController.delegate = self;
myPickerController.sourceType = UIImagePickerController.SourceType.photoLibrary
self.present(myPickerController, animated: true, completion: nil)
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
myImageView.image = info[UIImagePickerController.InfoKey.originalImage] as? UIImage
myImageView.backgroundColor = UIColor.clear
self.dismiss(animated: true, completion: nil)
}
override func viewDidLoad() {
super.viewDidLoad()
}
func generateBoundaryString() -> String
{
return "Boundary-\(UUID().uuidString)"
}
func myImageUploadRequest()
{
let myUrl = NSURL(string: "http://localhost/~*******/PHP/App/scripts/imageUpload.php");
//let myUrl = NSURL(string: "http://www.boredwear.com/utils/postImage.php");
let request = NSMutableURLRequest(url:myUrl! as URL);
request.httpMethod = "POST";
let param = [
"firstName" : "Sergey",
"lastName" : "Kargopolov",
"userId" : "9"
]
let boundary = generateBoundaryString()
request.setValue("multipart/form-data; boundary=\(boundary)", forHTTPHeaderField: "Content-Type")
let imageData = myImageView.image!.jpegData(compressionQuality: 1)
if(imageData==nil) {
return;
}
print("imageData =\(imageData as Any)")
//request.HTTPBody = createBodyWithParameters(param, filePathKey: "file", imageDataKey: imageData!, boundary: boundary)
request.httpBody = createBodyWithParameters(parameters: param, filePathKey: "file", imageDataKey: imageData! as NSData, boundary: boundary) as Data
myActivityIndicator.startAnimating();
let task = URLSession.shared.dataTask(with: request as URLRequest) {
data, response, error in
if error != nil {
print("error=\(String(describing: error))")
return
}
// You can print out response object
print("******* response = \(String(describing: response))")
// Print out reponse body
let responseString = NSString(data: data!, encoding: String.Encoding.utf8.rawValue)
print("****** response data = \(responseString!)")
do {
let json = try JSONSerialization.jsonObject(with: data!, options: []) as? NSDictionary
print(json as Any)
DispatchQueue.main.async {
self.myActivityIndicator.stopAnimating()
self.myImageView.image = nil;
}
}catch
{
print(error)
}
}
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 = "user-profile.jpg"
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")
return body
}
}
extension NSMutableData {
func appendString(string: String) {
//let data = string.data(using: String.Encoding.utf8, allowLossyConversion: true)
let data = string.data(using: String.Encoding.utf8, allowLossyConversion: true)
append(data!)
}
}
error message:
imageData =Optional(2322803 bytes)
2022-12-12 04:50:44.756347+0300 ImageUploadExample[7201:311148] [] nw_protocol_get_quic_image_block_invoke dlopen libquic failed
******* response = Optional(<NSHTTPURLResponse: 0x6000031e55a0> { URL: http://localhost/~*******/PHP/App/scripts/imageUpload.php } { Status Code: 200, Headers {
Connection = (
"Keep-Alive"
);
"Content-Type" = (
"text/html; charset=UTF-8"
);
Date = (
"Mon, 12 Dec 2022 01:50:44 GMT"
);
"Keep-Alive" = (
"timeout=5, max=100"
);
Server = (
"Apache/2.4.48 (Unix) PHP/8.1.13"
);
"Transfer-Encoding" = (
Identity
);
"X-Powered-By" = (
"PHP/8.1.13"
);
} })
****** response data = {"Message":"Sorry, there was an error uploading your file.","Status":"Error","userId":"9"}
Optional({
Message = "Sorry, there was an error uploading your file.";
Status = Error;
userId = 9;
})
I have an App where I take a picture from the camera and it has to be send to a server (PHP), the problem is, that when I upload the picture, the server only receives a tmp file instead. How can I solve this? I thank your answers in advance. My code is the following:
Take Photo Button
#IBAction func tomarFoto(_ sender: Any) {
indicadorActividad.startAnimating()
let image = UIImagePickerController()
image.delegate = self
image.sourceType = UIImagePickerControllerSourceType.camera
image.allowsEditing = true
self.present(image, animated: true){
}
}
Change size of picture
#IBAction func tomarFoto(_ sender: Any) {
indicadorActividad.startAnimating()
let image = UIImagePickerController()
image.delegate = self
image.sourceType = UIImagePickerControllerSourceType.camera
image.allowsEditing = true
self.present(image, animated: true){
}
}
ImagePicker
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
if let image = info[UIImagePickerControllerOriginalImage] as? UIImage {
imageView.image = resizedImage(image: image, newWidth: 300, newHeight: 300)
} else {
//Algo
}
//Detenemos el indicador y se cierra la ventana de la camara/galeria
self.indicadorActividad.stopAnimating()
self.dismiss(animated: true, completion: nil)
}
func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
picker.dismiss(animated: true, completion: nil)
}
The following function takes the picture and Uploads it to the PHP server, I think the error might be here.
Upload to server
func subirFotoRequest(){
let url = URL(string: "http://192.168.0.155/BolsaTrabajo/imagen.php")
let request = NSMutableURLRequest(url: url!)
request.httpMethod = "POST"
let boundary = generateBoundaryString()
request.setValue("multipart/form-data; boundary=\(boundary)", forHTTPHeaderField: "Content-Type")
if (imageView.image == nil)
{
return
}
let image_data = UIImagePNGRepresentation(imageView.image!)
if(image_data == nil)
{
return
}
let body = NSMutableData()
indicadorActividad.startAnimating()
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)
//self.indicadorActividad.stopAnimating()
do {
DispatchQueue.main.async(execute: {
self.indicadorActividad.stopAnimating()
self.imageView.image = nil;
});
}
catch{
//Errores
}
}
task.resume()
}
func generateBoundaryString() -> String
{
return "Boundary-\(UUID().uuidString)"
}
And the final block of code is my PHP script.
PHP File [UPDATED]
PHP file now uploads the picture in the respective format, the only problem is, it overrides my previous picture. How can I prevent this from happen? I was thinking probably by setting a name that will change for every picture in my swift class. Code updated is the following Previous code is commented:
//$ruta = "Imagenes/";
//if($_FILES["file"]["name"] !== ""){
// mkdir($ruta, 0777, true);
// move_uploaded_file($_FILES["file"]["name"], $ruta.'/'.
//basename($_FILES["file"]["name"]));
// echo "subido";
//}
$fichero_subido = $dir_subida . basename($_FILES['file']['name']);
echo '<pre>';
if(move_uploaded_file($_FILES['file']['tmp_name'], $fichero_subido)){
echo "El fichero es válido y se subó con éxito \n";
}
else{
echo "Posible ataque de subida de ficheros\n";
}
echo 'Más información de depuración: ';
print_r($_FILES);
I am trying to use Google Vision API and upload an image using their API to get analysis. I am using this php code:
<?php
include("./includes/common.php");
include_once("creds.php"); // Get $api_key
$cvurl = "https://vision.googleapis.com/v1/images:annotate?key=" . $api_key;
$type = "LABEL_DETECTION";
//echo "Item is: " . $item;
//Did they upload a file...
$item = $_GET[item];
if($_FILES['photo']['name'])
{
}else{
echo "you did not upload image".
}
It always show "you did not upload image". And here's my Swift function where I upload the image:
func UploadRequest(img: UIImage, item: String)
{
let url = NSURL(string: "http://myurlhere")
let request = NSMutableURLRequest(URL: url!)
request.HTTPMethod = "POST"
let boundary = generateBoundaryString()
//define the multipart request type
request.setValue("multipart/form-data; boundary=\(boundary)", forHTTPHeaderField: "Content-Type")
let image_data = UIImagePNGRepresentation(img)
if(image_data == nil)
{
return
}
let body = NSMutableData()
let fname = "image.png"
let mimetype = "image/png"
//define the data post parameter
body.appendData("--\(boundary)\r\n".dataUsingEncoding(NSUTF8StringEncoding)!)
body.appendData("Content-Disposition:form-data; name=\"test\"\r\n\r\n".dataUsingEncoding(NSUTF8StringEncoding)!)
body.appendData("hi\r\n".dataUsingEncoding(NSUTF8StringEncoding)!)
body.appendData("--\(boundary)\r\n".dataUsingEncoding(NSUTF8StringEncoding)!)
body.appendData("Content-Disposition:form-data; name=\"file\"; filename=\"\(fname)\"\r\n".dataUsingEncoding(NSUTF8StringEncoding)!)
body.appendData("Content-Type: \(mimetype)\r\n\r\n".dataUsingEncoding(NSUTF8StringEncoding)!)
body.appendData(image_data!)
body.appendData("\r\n".dataUsingEncoding(NSUTF8StringEncoding)!)
body.appendData("--\(boundary)--\r\n".dataUsingEncoding(NSUTF8StringEncoding)!)
request.HTTPBody = body
let session = NSURLSession.sharedSession()
let task = session.dataTaskWithRequest(request) {
(
let data, let response, let error) in
guard let _:NSData = data, let _:NSURLResponse = response where error == nil else {
//EZLoadingActivity.hide(success: false, animated: true)
print("error")
return
}
let dataString = NSString(data: data!, encoding: NSUTF8StringEncoding)
print(dataString)
//EZLoadingActivity.hide(success: true, animated: true)
self.dismissViewControllerAnimated(true, completion: nil)
}
task.resume()
}
When I do print_r($_FILES), I get:
Array
(
[file] => Array
(
[name] => image.png
[type] => image/png
[tmp_name] => /tmp/phplSB2dc
[error] => 0
[size] => 864781
)
)
Your form data is currently submitting:
body.appendData("Content-Disposition:form-data; name=\"file\";...
And according to your print_r($_FILES), you should be using file instead of photo:
$_FILES['file']['name']
Also, you should be checking to make sure the file uploaded correctly using:
if ( $_FILES['file']['error'] == UPLOAD_ERR_OK )
{
//File uploaded correctly
}
func uploadMultipleIMAGE( APIStriing:NSString, dataString:NSString) -> Void
{
//Here dataString is image binary data just append you your url and just pass the image it will handle.
print("wscall URL string \(APIStriing)")
print("Data string URL string \(dataString)")
let postData = dataString.dataUsingEncoding(NSASCIIStringEncoding, allowLossyConversion: true)!
let postLength = "\(UInt(postData.length))"
let request = NSMutableURLRequest()
request.URL = NSURL(string: APIStriing as String)!
request.HTTPMethod = "POST"
request.setValue(postLength, forHTTPHeaderField: "Content-Length")
request.setValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")
request.HTTPBody = postData
NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue.mainQueue(), completionHandler: {(response: NSURLResponse?,data: NSData?,error: NSError?) -> Void in
if error == nil {
print("success")
}
else {
let alert = UIAlertView(title: "Sorry For Inconvenience ", message: "Contact to Administration", delegate: self, cancelButtonTitle: "Ok")
alert.show()
}
})
}
Note:- when you convert image to binary data you need to handle special character. here you can handle special character. follow this step.
1.
let imgData = UIImagePNGRepresentation(image)
imgBase64String = imgData!.base64EncodedStringWithOptions(NSDataBase64EncodingOptions())
var newimagdat = NSString()
newimagdat = newimagdat.stringByAppendingString(self.percentEscapeString(imgBase64String) as String)
imgarray.addObject(newimagdat)
Create character handler class something like that.
class handlecharacter: NSCharacterSet {
func urlParameterValueCharacterSet() -> NSCharacterSet {
let characterser = NSMutableCharacterSet.alphanumericCharacterSet()
characterser.addCharactersInString("-._~")
return characterser
}
}
Now create function your class where you want to upload images
func percentEscapeString(imgstring:NSString) -> NSString
{
let handle = handlecharacter()
return imgstring.stringByAddingPercentEncodingWithAllowedCharacters(handle.urlParameterValueCharacterSet())!
}
Hello I have a small a problem with the code below, Xcode print this error:
ERROR: json error: Error Domain=NSCocoaErrorDomain Code=3840 "No
value." UserInfo={NSDebugDescription=No value.}
SWIFT CODE:
let yourUrl=“mylink.php”
let URL = NSURL(string:yourUrl)
let request:NSMutableURLRequest = NSMutableURLRequest(URL: URL!)
let boundaryConstant = "V2ymHFg03esomerandomstuffhbqgZCaKO6jy";
let contentType = "multipart/form-data; boundary=" + boundaryConstant
NSURLProtocol.setProperty(contentType, forKey: "Content-Type", inRequest: request)
let dataString = "Email=\(Email.text)&Password=\(Password.text)"
request.HTTPMethod = "POST"
request.HTTPBody = (dataString as NSString).dataUsingEncoding(NSUTF8StringEncoding)
let task = NSURLSession.sharedSession().dataTaskWithRequest(request, completionHandler: { (data, response, error) -> Void in
//reponse recieved
//in this case response string will be saved in data, its of type NSData
do{
let str = try NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions.AllowFragments) as! [String:AnyObject]
print(str)
}
catch {
print("json error: \(error)")
}
})
task.resume()
PHP CODE:
mylink.php
<?php
header("Content-Type: application/json");
$email = $_POST["Email"];
$password = $_POST["Password"];
$stat=“myvalue”;
return json_encode($stat);
?>
How can I solve this error?
i try to send value to server with this code by i have an error on this line:
let reply = NSURLConnection.sendSynchronousRequest(request, returningResponse:&response, error: &error)
the error is : EXTRA ARGUMENT IN CALL "error"
var yourUrl="mylink"
let url = NSURL(string:yourUrl)
let cachePolicy = NSURLRequestCachePolicy.ReloadIgnoringLocalCacheData
var request = NSMutableURLRequest(URL: url!, cachePolicy: cachePolicy, timeoutInterval: 2.0)
request.HTTPMethod = "POST"
// set Content-Type in HTTP header
let boundaryConstant = "
V2ymHFg03esomerandomstuffhbqgZCaKO6jy";
let contentType = "multipart/form-data; boundary=" + boundaryConstant
NSURLProtocol.setProperty(contentType, forKey: "Content-Type", inRequest: request)
// set data
var dataString = "my value"
let requestBodyData = (dataString as NSString).dataUsingEncoding(NSUTF8StringEncoding)
request.HTTPBody = requestBodyData
var response: NSURLResponse? = nil
var error: NSError? = nil
let reply = NSURLConnection.sendSynchronousRequest(request, returningResponse:&response, error: &error)
let results = NSString(data:reply!, encoding:NSUTF8StringEncoding)
if let dataFromString = results!.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false) {
let json = JSON(data: dataFromString)
if(json=="0"){
print("\n Dati non validi");
}
else{
print("\n Account creato");
}
}
You should never use sendSynchronousRequest as it is a blocking call. Also it is deprecated from iOS 9.0 onwards. Instead you can use NSURLSession. Try out following code
var yourUrl="mylink"
let URL = NSURL(string:yourUrl)
let request:NSMutableURLRequest = NSMutableURLRequest(URL: URL)
let boundaryConstant = "V2ymHFg03esomerandomstuffhbqgZCaKO6jy";
let contentType = "multipart/form-data; boundary=" + boundaryConstant
NSURLProtocol.setProperty(contentType, forKey: "Content-Type", inRequest: request)
var dataString = "my value"
request.HTTPMethod = "POST"
request.HTTPBody = (dataString as NSString).dataUsingEncoding(NSUTF8StringEncoding)
let task = NSURLSession.sharedSession().dataTaskWithRequest(request, completionHandler: { (data, response, error) -> Void in
//reponse recieved
//in this case response string will be saved in data, its of type NSData
do{
let str = try NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions.AllowFragments) as! [String:AnyObject]
print(str)
}
catch {
print("json error: \(error)")
}
})
task.resume()