Main Menu

Login Form



Syndication

feed-image


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.

 

 

include('lib.php');

//get token for user
$login='mylogin';
$password='mypassword';

if(!getToken($login,$password)){
 echo 'Can not get token. Maybe login/password incorrect';
 exit;
}

//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
$fileid=uploadFile('./angel2.jpg','Angel2.jpg',$folderid);
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;
 
 $data=array(
 'fi_pid'=>$parentid,        //file parent folder id
 'file_name1'=>$filename,    //file name
 'file_desc1'=>'',        //description and tags are empty for this file
 'file_tags1'=>'',
 '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