php - 使用带有PHP的google drive api将文件上传到特定文件夹

标签 php google-drive-api google-api-php-client

我正在使用 google drive api 上传文件, 到目前为止,这是我的代码 现在文件上传到我的根目录,我想将路径更改为另一个文件夹,

   <?php
        error_reporting(E_ALL);
        ini_set('display_errors', 1);
        require_once 'google-api-php-client/src/Google_Client.php';
        require_once 'google-api-php-client/src/contrib/Google_DriveService.php';
        $client = new Google_Client();
        // Get your credentials from the APIs Console
        $client->setClientId('***');
        $client->setClientSecret('***');
        $client->setRedirectUri('http://***');
        $client->setScopes(array('https://www.googleapis.com/auth/drive'));

        if(empty($_GET['code']))
        {
            $client->authenticate();
        }

        $myfile= "video.mp4";
        $type='video/mp4';

        $service = new Google_DriveService($client);

        // Exchange authorization code for access token
        $accessToken = $client->authenticate($_GET['code']);
        $client->setAccessToken($accessToken);


        //Insert a file

        $file = new Google_DriveFile();
        $file->setTitle('filename.mp4');
        $file->setDescription('A video');
        $file->setMimeType($type);

        $data = file_get_contents($myfile);

        $createdFile = $service->files->insert($file, array(
              'data' => $data,
              'mimeType' => $type,
            ));

        print_r($createdFile);
        echo "<br />";

我尝试了一些来自 stackoverflow 的帖子 他们都没有为我工作我得到了错误, 提前致谢

最佳答案

Google_Service_Drive_ParentReference() 类似乎不再有效。但是,以下代码对我有用:

$fileMetadata = new Google_Service_Drive_DriveFile(array(
        //Set the Random Filename
        'name' => $rdmName,
        //Set the Parent Folder
        'parents' => array('0BzaRxkCDivjIM1pUYU1SLWVxOGM') // this is the folder id
    ));

    try{

        $newFile = $drive_service->files->create(
            $fileMetadata,
            array(
                'data' => file_get_contents(TESTFILE),
                'mimeType' => 'application/pdf',
                'uploadType' => 'media'
            )
        );


    } catch(Exception $e){

        echo 'An error ocurred : ' . $e->getMessage();

    }

关于php - 使用带有PHP的google drive api将文件上传到特定文件夹,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18363045/

相关文章:

javascript相当于php的for循环效率

PHP - 数组排序

google-apps-script - 列出 Google 云端硬盘上特定演示文稿/文档的所有共享用户

google-drive-api - 是否可以在不访问整个驱动器或使用驱动器应用程序的情况下访问单个谷歌驱动器文件?

android - 如何使用 android auto backup 以编程方式恢复和备份

php - 关于在 php 中打印二维数组

php - 如何在多个页面上使用 Google api?

php - 使用 cron 执行 PHP 将文件上传到 Google 驱动器

php - 如何通过 Google 日历 php api 使用扩展属性

php - 从 id=id 的其他表获取信息