使用带有PHP的google驱动器api将文件上传到特定文件夹

发布时间:2022-04-30 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了使用带有PHP的google驱动器api将文件上传到特定文件夹脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在使用谷歌驱动器api上传文件,
到目前为止这是我的代码
现在文件上传到我的root,我想更改到另一个文件夹的路径,
<?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的一些帖子
并没有一个为我工作我得到了错误,
提前致谢

像这样设置父文件夹:
//Set the Parent Folder
$parent = new Google_Service_Drive_ParentReference(); //prevIoUsly Google_ParentReference
$parent->setId('0B9mYBlahBlahBlah');
$file->setParents(array($parent));

脚本宝典总结

以上是脚本宝典为你收集整理的使用带有PHP的google驱动器api将文件上传到特定文件夹全部内容,希望文章能够帮你解决使用带有PHP的google驱动器api将文件上传到特定文件夹所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。