Search code examples
phpgoogle-apigoogle-drive-apigoogle-api-php-client

Upload Files In Folders Using Google Drive API


I currently have the below code that can successfully upload a file to google drive in the root directory. How would I have to change the below code such that if $folderName exist, it uploads the file under that folder but if $folderName doesn't exists, it creates the folder, calls it $folderName, and then adds the file underneath it.

function uploadFiles($filePath, $fileName, $folderName) {
    $file = new Google_Service_Drive_DriveFile();
    $file->setName($fileName);
    $file->setDescription('A test document');
    
    $data = file_get_contents($filePath);
    
    $createdFile = $this->service->files->create($file, array(
        'data' => $data,
        'uploadType' => 'multipart'
    ));
}

Solution

  • I believe your goal and situation as follows.

    • You want to upload a file to the specific folder using googleapis for php.
    • When the specific folder is not existing, you want to create the folder and upload the file to the folder.
    • When the specific folder is existing, you want to upload the file to the folder.
    • You have already been able to upload a file to Google Drive using Drive API.

    Modification points:

    • In this case, at first, it is required to confirm whether the specific folder is existing. So in this case, the method of "Files: list" in Drive API is used. So the flow of the modified script is as follows.

      1. Search the existing folder using the folder name.
      2. When the folder of the folder name is NOT existing, the folder is created by the folder name and the folder ID of the created folder is returned.
        • In this case, the method of "Files: create" is used.
      3. When the folder of the folder name is existing, the folder ID is returned.
      4. The file is uploaded to the folder using the folder ID.

    Modified script:

    function uploadFiles($filePath, $fileName, $folderName) {
        // 1. Search the existing folder using the folder name.
        $res = $this->service->files->listFiles(array("q" => "name='{$folderName}' and trashed=false"));
        $folderId = '';
        if (count($res->getFiles()) == 0) {
            // 2. When the folder of the folder name is NOT existing, the folder is created by the folder name and the folder ID of the created folder is returned.
            $file = new Google_Service_Drive_DriveFile();
            $file->setName($folderName);
            $file->setMimeType('application/vnd.google-apps.folder');
            $createdFolder = $this->service->files->create($file);
            $folderId = $createdFolder->getId();
        } else {
            // 3. When the folder of the folder name is existing, the folder ID is returned.
            $folderId = $res->getFiles()[0]->getId();
        }
    
        // 4. The file is uploaded to the folder using the folder ID.
        $file = new Google_Service_Drive_DriveFile();
        $file->setName($fileName);
        $file->setDescription('A test document');
        $file->setParents(array($folderId));
        $data = file_get_contents($filePath);
        $createdFile = $this->service->files->create($file, array(
            'data' => $data,
            'uploadType' => 'multipart'
        ));
    }
    

    References: