Login Form



Uploading file to SMEStorage PDF Print E-mail

In this post i will describe php sample that uploads file to SMEStorage (http://www.smestorage.com)

In previous post  i wroute about how SMEStorage API works in general and creating of folder was described.

Now i will extend that sample and add function for uploading of the file.




//get token for user

 echo 'Can not get token. Maybe login/password incorrect';

//upload file to root folder
$fileid=uploadFile('./angel1.jpg','Test image','0');
echo 'File uploaded with ID '.$fileid.'<br>';

//create folder in root
$folderid=createFolder('My images','0');
echo 'Folder created with ID '.$folderid.'<br>';

//upload file to just created folder My images
echo '2-nd file uploaded with ID '.$fileid.'<br>';

This is test file code.  There are 2 callings of uploadFile function. This function is described in lib.php file.

On first call of the uploadFile function the file angel1.jpg is uploaded to root folder of SMEStorage account. With 2-nd call the file angel2.jpg is uploaded to just created folder.

Body of function uploadFile

function uploadFile($filepath,$filename,$parentid='0'){
 global $token;
 'fi_pid'=>$parentid,        //file parent folder id
 'file_name1'=>$filename,    //file name
 'file_desc1'=>'',        //description and tags are empty for this file
 'file_1'=>'@'.$filepath        //path to file. @ means that this file must be inserted in the post request

 $a= processRequest($token.'/doUploadFiles/',$data);

 if($a[0]=='' && $a[1]['files']['n1']['status']=='ok'){
 //return new file id
 return $a[1]['files']['n1']['response']['file']['fi_id'];
 return 0;

The function prepares list of arguments for POST request to the API server.

Take a look at argument 'file_1'=>'@'.$filepath .

@ - is directive for cUrl library that it is needed to include file by the path to POST request.

There is zip of the sample with described code - download

Last Updated on Thursday, 18 February 2010 14:43