I am building a mobile site that runs from an API and have an API CALL handler class which does all the calls which I run from a primary functions file.
The issue here is my files are not being sent through to the API and it's not recognising what is a file and is returning a file not present error.
NOTE: ISSUE SOLVED AND WORKING CODE BELOW
Code Below:
FORM
<form id="uploadPhoto" action="<?php uploadStreamPhoto(); ?>" method="post" enctype="multipart/form-data">
<input type="file" name="streamPhotoUpload" id="streamPhotoUpload" />
<input type="submit" name="streamPhotoUploadSubmit" id="streamPhotoUploadSubmit" value="Upload" />
</form>
UPLOAD FUNCTION
function uploadStreamPhoto()
{
if(isset($_POST['streamPhotoUploadSubmit']))
{
$apiHandler = new APIHandler();
$result = $apiHandler->uploadStreamPhoto($_FILES['streamPhotoUpload']['tmp_name']);
$json = json_decode($result);
var_dump($json);
//header('Location: '.BASE_URL.'stream-upload-preview');
}
}
HANDLER METHOD
public function uploadStreamPhoto($file)
{
$result = $this->request(API_URL_ADD_PHOTO, array(
'accessToken' => $this->accessToken,
'file' => "@$file;filename=".time().".jpg",
'photoName' => time(),
'albumName' => 'Stream'
));
return $result;
}
CURL REQUEST METHOD
/**
* Creates a curl request with the information passed in post fields
*
* @access private
* @param string $url
* @param array $postFields
* @return string
**/
private function request($url, $postFields = array())
{
$curl = curl_init();
//Check the SSL Matches the host
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
if($this->debug == true)
{
//Prevent curl from verifying the certificate
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
}
//Set the URL to call
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_HEADER, 0);
//Set the results to be returned
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
//Set the curl request as a post
curl_setopt($curl, CURLOPT_POST, 1);
//Set the post fields
curl_setopt($curl, CURLOPT_POSTFIELDS, $postFields);
$result = curl_exec($curl);
if($result === false)
{
$result = 'Curl error: '.curl_error($curl);
}
curl_close($curl);
return $result;
}