繁体   English   中英

使用PHP将文件上传到Google云端硬盘文件夹

[英]Uploading a file into a Google Drive folder with 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;
}

如果有人有建议,请告诉我!

谢谢

为此,您已按所需顺序插入了文件夹。 因此,将统计信息添加到Drive根文件夹下,然后按所需顺序添加所有文件夹。 要添加文件夹,您需要将mimeType as 'application/vnd.google-apps.folder' 检查此链接以获取更多mimeType值。 这是有关如何在云端硬盘中插入文件夹的外部参考链接

添加所有必需的文件夹后,您现在可以在{Username}文件夹下插入实际文件。 您也可以参考此页面,了解如何在云端硬盘中插入文件。

希望有帮助!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM