Last active
November 5, 2017 04:27
-
-
Save kinokoRider/97f4635e931397b41e19e8aee6be68cd to your computer and use it in GitHub Desktop.
DropBoxWrapper.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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; | |
} | |
} | |
} | |
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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