Instantly share code, notes, and snippets.

Embed
What would you like to do?
DropBoxWrapper.php
<?php
//このプログラムは kunalvarma05/dropbox-php-sdk を用いて、
//PHPでDropBoxを扱うためのラッパークラスです。
//参考:https://github.com/kunalvarma05/dropbox-php-sdk
use Kunnu\Dropbox\Dropbox;
use Kunnu\Dropbox\DropboxApp;
use Kunnu\Dropbox\DropboxFile;
use Kunnu\Dropbox\Exception;
class OperateDropbox{
private $accessToken = 'your-DropBox-APPs-AccessToken';
private $key = 'your-DropBox-APPKey';
Private $secret = 'your-DropBox-APPsecret';
private $dropbox;
//DropBoxに接続するクライアント作成
public function MakeClient(){
$app = new DropboxApp($this->key,
$this->secret, $this->accessToken);
$this->dropbox = new Dropbox($app);
}
//DropBoxにファイル保存
public function UploadToDropBox($filePathOnDropBox, $filePathOnLocal){
try{
$dropboxFile = new DropboxFile($filePathOnLocal);
$file = $this->dropbox->upload($dropboxFile,
$filePathOnDropBox, ['autorename' => true]);
return true;
}catch(DropboxClientException $ex){
return false;
}
}
//Dropboxからファイルをダウンロード
public function DownLoadToLocal($filePathOnDropBox, $filePathOnLocal){
try{
$file = $this->dropbox->download($filePathOnDropBox);
$content = $file->getContents();
file_put_contents($filePathOnLocal, $content);
$metadata = $file->getMetadata();
$metadata->getName();
return true;
}catch(DropboxClientException $ex){
return false;
}
}
//Dropboxからファイル削除
public function DeleteFromDropBox($filePathOnDropBox){
try{
$res = $this->dropbox->delete($filePathOnDropBox);
return true;
}catch(DropboxClientException $ex){
return false;
}
}
}
?>
<?php
$operateDropBox = new OperateDropbox();
$operateDropBox->MakeClient();
$operateDropBox->anyFunction();
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment