12

我正在使用 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的帖子,但没有一个对我有用,我得到了错误,在此先感谢

4

4 回答 4

12

看来这门课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();

    }
于 2016-08-26T18:52:44.953 回答
4

像这样设置父文件夹:

//Set the Parent Folder
$parent = new Google_Service_Drive_ParentReference(); //previously Google_ParentReference
$parent->setId('0B9mYBlahBlahBlah');
$file->setParents(array($parent));
于 2013-11-09T11:53:31.327 回答
3

设置父 ID 列表应该有效。我们的公共文档包含每个 API 方法的工作片段。请先看看它们:https ://developers.google.com/drive/v2/reference/files/insert

$file->setParents([{'id': parent_id}])
于 2013-08-22T18:42:37.137 回答
1

使用下面的代码。在谷歌驱动器中插入文件非常有效。

<?php
require_once 'Google/autoload.php';
require_once 'Google/Client.php';
require_once 'Google/Service/Drive.php';

$client = new Google_Client();
// Get your credentials from the console
$client->setClientId('your client id');
$client->setClientSecret('your client secret');
$client->setRedirectUri('redirect uri set on google apps');
$client->setScopes(array('https://www.googleapis.com/auth/drive.file'));

session_start();

if (isset($_GET['code']) || (isset($_SESSION['access_token']) &&    $_SESSION['access_token'])) {

    if (isset($_GET['code'])) {
        $client->authenticate($_GET['code']);
        $_SESSION['access_token'] = $client->getAccessToken();
    } 
    else
        $client->setAccessToken($_SESSION['access_token']);

    $service = new Google_Service_Drive($client);

    //Insert a file
    $file = new Google_Service_Drive_DriveFile();
    $file->setName(uniqid().'.jpg');
    $file->setDescription('A test document');
    $file->setMimeType('image/jpeg');

    $data = file_get_contents('a.jpg');

    $createdFile = $service->files->create($file, array(
      'data' => $data,
      'mimeType' => 'image/jpeg',
      'uploadType' => 'multipart'
    ));

    print_r($createdFile);
} 
else {
    $authUrl = $client->createAuthUrl();
    header('Location: ' . $authUrl);
    exit();
}
?>
于 2015-07-21T05:35:39.487 回答