skip to Main Content

I can upload regular files. No problem here.
I get an error when updating existing files
I added ‘uploadType’ => ‘multipart’ and it gave an error again.

There is a ".htaccess" file in the directory and it says "deny from all" in its content.
I delete it from the ".htaccess" drive and it gives an error in another file.

An example error message:
GoogleServiceDriveResourceFiles->update( $fileId = '1ouvREdK6mJSOu_AqGyffcuwFvmvHM4Ar', $postBody = NULL, $optParams = 'deny from all', [] )
Error in another file shows file content

second line gives error
$content = file_get_contents($filePath); $service->files->update($existingFile->getId(), null, $content, []);

I am using a Google Drive Service Account
and I am using the php 8.2 version library

2

Answers


  1. Chosen as BEST ANSWER

    My English is very poor.

    My full code is below.

    Loading a directory and all its contents, even subdirectories and subfiles, works smoothly.

    The problem here is:

    Error while overwriting existing file, cannot overwrite existing file.

    `// Folder and sub-content loading function

    function uploadFolder($service, $parentId, $folderPath) {
        $folderName = basename($folderPath);
    
        // Check if folder exists
        
        $existingFolder = searchFile($service, $parentId, $folderName);
    
        if ($existingFolder) {
            $createdFolder = $existingFolder;
        } else {
        
            // Create folder
            
            $folder = new Google_Service_Drive_DriveFile();
            $folder->setName($folderName);
            $folder->setMimeType('application/vnd.google-apps.folder');
            $folder->setParents([$parentId]);
    
            $createdFolder = $service->files->create($folder);
        }
    
        // Upload files within folder
        
        $files = scandir($folderPath);
        foreach ($files as $file) {
            if ($file != '.' && $file != '..') {
                $filePath = $folderPath . '/' . $file;
    
                if (is_dir($filePath)) {
                
                    // If file is a folder, load subfolder
                    
                    uploadFolder($service, $createdFolder->id, $filePath);
                } else {
                
                    // If the file is a file, upload the file
                    
                    $existingFile = searchFile($service, $createdFolder->id, $file);
    
                    if ($existingFile) {
                    
                        // If the file already exists, overwrite it
                        
                        $content = file_get_contents($filePath);
                        $service->files->update($existingFile->getId(), null, $content);
    
                        echo "<span style='color: red'>The file was overwritten:</span> ".$filePath."<br />";
                    } else {
                    
                        // If file does not exist, create new file
                        
                        $fileMetadata = new Google_Service_Drive_DriveFile();
                        $fileMetadata->setName($file);
                        $fileMetadata->setParents([$createdFolder->id]);
    
                        $content = file_get_contents($filePath);
                        $createdFile = $service->files->create($fileMetadata, [
                            'data' => $content,
                            'mimeType' => 'application/octet-stream',
                            'uploadType' => 'multipart',
                        ]);
                        echo "<span style='color: blue;'>File uploaded:</span> ".$filePath."<br />";
                    }
                }
            }
        }
    }
    
    // Searching for files in a specific folder
    
    function searchFile($service, $parentId, $fileName) {
        $results = $service->files->listFiles([
            'q' => "name='".$fileName."' and '".$parentId."' in parents",
        ]);
    
        if (count($results->getFiles()) > 0) {
            return $results->getFiles()[0];
        } else {
            return null;
        }
    }`
    

    enter image description here


  2. I solved the error of overwriting the existing file as follows.

    It didn’t give an error, but did it actually overwrite the file?

                        if ($existingFile) {
                    
                    // If the file already exists, overwrite it
                    
                    $existing_File = new Google_Service_Drive_DriveFile();
                    $service->files->update($existingFile->getId(), $existing_File, array(
                        'data' => $filePath,
                        'mimeType' => mime_content_type($filePath),
                        'uploadType' => 'multipart'
                    ));
    
                        echo "<span style='color: red'>The file was overwritten:</span> ".$filePath."<br />";
                    } else {
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search