简体   繁体   中英

How can i upload file to a specific folder in google drive -Google drive API (PHP)

How can i upload file to a specific folder in google drive -Google drive API? I have this code, and it upload my file to the main folder in google drive.

$client->setAccessToken($_SESSION['accessToken']);
$service = new Google_DriveService($client);
$finfo = finfo_open(FILEINFO_MIME_TYPE);
$file = new Google_DriveFile();
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);
    $service->files->insert($file, array(
        'data' => file_get_contents($file_path),
         'mimeType' => $mime_type
    ));
}

Here's how you do it in V3. It's called "setting a parent" since once the file is inside the folder it will also inherit the sharing options of the parent it's in.

Below 2 lines will set the name of the file and then setParents will define what folder you want to put the file in. Parameter is an array but don't put more than 1 item or you'll get an error:

$file->setName("world");
$file->setParents(['SOME_UNIQUE_FOLDER_ID']);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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