<?php
class CurlFileUploader {
var $filePath;
var $uploadURL;
var $formFileVariableName;
var $postParams = array();
/* Constructor for CurlFileUploader
* @param $filePath absolute path of file
* @param $uploadURL url where you want to upload file
* @param $formFileVariableName form field name to upload file
* @param $otherParams assosiative array of other params which you want to send as post
*/
function CurlFileUploader ($filePath, $uploadURL, $formFileVariableName, /* assosiative array */ $otherParams = false) {
$this->filePath = $filePath;
$this->uploadURL = $uploadURL;
if(is_array($otherParams) && $otherParams != false) {
foreach ($otherParams as $fieldName => $fieldValue) {
$this->postParams[$fieldName] = $fieldValue;
}
}
$this->postParams[$formFileVariableName] = "/@".$filePath;
}
/*
* function to upload file
* if unable to upload file produce error and exit
* else upload file
*/
function UploadFile () {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $this->uploadURL );
curl_setopt($ch, CURLOPT_POST, 1 );
print_r($this->postParams);
curl_setopt($ch, CURLOPT_POSTFIELDS, $this->postParams);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST,'POST');
$postResult = curl_exec($ch);
if (curl_errno($ch)) {
print curl_error($ch);
print "<br>Unable to upload file.";
exit();
}
else
{
}
curl_close($ch);
exit();
}
}
?>
This is the class file and
the test file is as
<?php
// upload file
require_once("class/fileUploader.php");
$objCurlFileUploader = new CurlFileUploader("/home/abhik/Desktop/Clipboard01.jpg", "http://dev3.sqlfusion.com/workandtravel/fileUploaderTest.php",'file1');
$objCurlFileUploader->UploadFile();
// upload file with some post params
//$objCurlFileUploader = new CurlFileUploader("/home/abhik/Desktop/Clipboard01.jpg", "http://dev3.sqlfusion.com/workandtravel/fileUploaderTest.php",'file1', Array('test' => 'test1'));
//$objCurlFileUploader->UploadFile();
?>