无法将文件上传到谷歌驱动器中的父文件夹

时间:2018-05-10 07:54:01

标签: php

下面代码将所选文件上传到goolge驱动器的root,但是我希望它上传所选文件insilde不在root中的文件夹。我也传递了文件夹ID但它不起作用。

require_once 'google-api-php-client/src/Google_Client.php';
require_once 'google-api-php-client/src/contrib/Google_DriveService.php';

//echo "<pre>";
//print_r("ok");

//echo "<pre>";
//die();

$client = new Google_Client();
$client->setClientId('xxxxxxxxxxxxxxxx');
$client->setClientSecret('xxxxxxxxxxxxxxxx');
$client->setRedirectUri($url);
$client->setScopes(array('https://www.googleapis.com/auth/drive'));
if (isset($_GET['code'])) {
    $_SESSION['accessToken'] = $client->authenticate($_GET['code']);
    header('location:'.$url);exit;
} elseif (!isset($_SESSION['accessToken'])) {
    $client->authenticate();
}
$files= array();
$dir = dir('files');
while ($file = $dir->read()) {
    if ($file != '.' && $file != '..') {
        $files[] = $file;
    }
}
$dir->close();
if (!empty($_POST)) {
    $client->setAccessToken($_SESSION['accessToken']);
    $service = new Google_DriveService($client);
    $finfo = finfo_open(FILEINFO_MIME_TYPE);
    $file = new Google_DriveFile();
    $folderId="1dFcg7LbUsXsAfKn_IpntdffssMT92aQiskEr7p";


    foreach ($files as $file_name) {
        $file_path = 'files/'.$file_name;
        $mime_type = finfo_file($finfo, $file_path);
        $file->setTitle($file_name);
        $file->setDescription('This is a '.$mime_type.' document');
        $file->setMimeType($mime_type);
        // Set the parent folder.

          if ($folderId != null) {

             $parent = new Google_Service_Drive_ParentReference();


    //echo "<pre>";
    //print_r("ok"); 
    //echo "<pre>";
    //die();
  

我做了一个死();这里并没有在输出中打印“ok”,似乎这个类Google_Service_Drive_ParentReference()无效。

            $parent->setId($folderId);
            $file->setParents(array($parent));
          }else{

            echo "no folder found";
          }

        $service->files->insert(
            $file,
            array(
                'data' => file_get_contents($file_path),
                'mimeType' => $mime_type
            )
        );

        // Uncomment the following line to print the File ID
        // print 'File ID: %s' % $createdFile->getId();
    }

    finfo_close($finfo);
    header('location:'.$url);exit;
}

**表示赞赏:)

感谢**

0 个答案:

没有答案