Show Me How to Upload a text File From my SWIFT iOS app to PHP Using uploadTaskWithRequest

Completed Posted 7 years ago Paid on delivery
Completed Paid on delivery

Provide me the PHP code and Swift code for how to upload a text file from an app to a webserver using the uploadTaskWithRequest function.

Below is roughly what I have so far but it doesn't work. I get a successful response from the server but no file actually appears on the server or when I echo $_FILES its just blank.

lazy var config: NSURLSesionConfiguration = [login to view URL]()

lazy var session: NSURLSession = NSURLSession(configuration: [login to view URL], delgate: self, delegateQueue: [login to view URL]())

let phpURL = "[login to view URL]"

let uploadStringURL: NSURL = NSURL(string: phpURL)!

let uploadRequest = NSMutableURLRequest(URL: uploadStringURL)

let fileManager = [login to view URL]()

let docs = [login to view URL](.DocumentDirectory, inDomains: .userDomainMask).first

let fullFilePath = docs!/URLByAppendingPathComponent("[login to view URL]")

[login to view URL] = "POST"

let uploadTask = [login to view URL](uploadRequest, fromFile: fullFilePath, completionHandler: {(let data, let response, let error) in

})

[login to view URL]()

The uploadTask should be pushing the file to the server and it gets a response 200 showing that it was successful.

Here is the PHP part:

<?php

$target_dir = "/var/www/html/test-files/uploads";

$target_dir = $target_dir . "/" . basename($_FILES["file"]["name"]);

echo '{"target":'.$target_dir.'},';

echo '{"base":'.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"

]);

}

else {

echo json_encode([

"Message" => "Sorry, there was an error uploading your file.",

"Status" => "Error"

]);

}

?>

App Developer PHP Swift

Project ID: #10781683

About the project

2 proposals Remote project Active 7 years ago

Awarded to:

vaishakh22

iOS developer with 5+ years experience . Please check my recent reviews. I have good knowledge on php too. Please ping me for further details. Will get this working today itself

$25 USD in 1 day
(15 Reviews)
5.5

2 freelancers are bidding on average $25 for this job

alpeshtk

I am Alpesh . I am experienced developer from India and I am interested in your project. I have 6+ years of experience on PHP Development and Web Development. I have good experience on PHP Development . I have reviewe More

$25 USD in 1 day
(8 Reviews)
2.9