Jayson Tamayo Jayson Tamayo - 4 months ago 33
Swift Question

Image is not uploaded in PHP using Swift

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
)

)

Answer

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
}