2015-06-26 124 views
0

我已成功將文件上傳到Google雲端硬盤。不過,我仍然不確定如何將其上傳到文件夾中。我需要把它上傳到一個文件夾結構,看起來像這樣:使用PHP將文件上傳到Google雲端硬盤文件夾中

Stats 
    ACLLeauge 
    ACLSydney 
     Sorted 
     Unsorted 
      {Username} 
       {FileHere} 

{Username}領域是,我將通過一個變量。 {FileHere}字段是圖像需要去的地方。這是我目前的代碼:

public function __construct() 
{ 
    $this->instance = new \Google_Client(); 

    $this->instance->setApplicationName('DPStatsBot'); 
    $this->instance->setDeveloperKey(Config::getInstance()->getDriveDeveloperKey()); 
    $this->instance->setAuthConfigFile(Config::getInstance()->getClientSecret()); 
    $this->instance->addScope('https://www.googleapis.com/auth/drive'); 

    if(!file_exists(DP_STATS_BOT_DIR . '/' . Config::getInstance()->getAuthFile())) { 
     Printer::write('Please navigate to this URL and authenticate with Google: ' . PHP_EOL . $this->instance->createAuthUrl()); 
     Printer::raw('Authentication Code: '); 

     $code = trim(fgets(STDIN)); 
     $token = $this->instance->authenticate($code); 

     file_put_contents(DP_STATS_BOT_DIR . '/' . Config::getInstance()->getAuthFile(), $token); 

     Printer::write('Saved auth token'); 

     $this->instance->setAccessToken($token); 
    } 
    else 
    { 
     $this->instance->setAccessToken(file_get_contents(DP_STATS_BOT_DIR . '/' . Config::getInstance()->getAuthFile())); 
    } 

    if($this->instance->isAccessTokenExpired()) 
    { 
     $this->instance->refreshToken($this->instance->getRefreshToken()); 
     file_put_contents(DP_STATS_BOT_DIR . '/' . Config::getInstance()->getAuthFile(), $this->instance->getAccessToken()); 
    } 

    $this->drive_instance = new \Google_Service_Drive($this->instance); 
} 

public function upload($image, $dpname) 
{ 
    $file = new \Google_Service_Drive_DriveFile(); 
    $file->setTitle($dpname . '_' . RandomString::string() . '.jpg'); 

    $upload = $this->drive_instance->files->insert($file, 
    [ 
     'data' => $image, 
     'mimeType' => 'image/jpg', 
     'uploadType' => 'media' 
    ]); 

    return $upload; 
} 

如果有人有一個建議,請告訴我!

謝謝

回答

1

爲此,您已按所需順序插入文件夾。因此,請在Drive根文件夾下添加統計信息,然後按照需要的順序添加所有文件夾。要添加文件夾,您需要提供mimeType as 'application/vnd.google-apps.folder'。檢查此link以獲取更多mimeType值。以下是關於如何在雲端硬盤中插入文件夾的外部引用link

添加所有必需的文件夾後,您現在可以在{用戶名}文件夾下插入實際文件。您也可以參考此頁面瞭解如何在Drive中輸入insert文件。

希望有幫助!

相關問題