I am trying to make a function for renaming a file in google drive using the api:v2 in php. But every the function patch that is said to be used to rename the files does not exist in the drives file of google's in '\google\apiclient-services\src\Drive\Resource\Drives.php' file. I did search in other files but it was not there. Now how can I rename the files and folder in my drive. I did try to change some.
The code that I got from the documentation was:-
/**
* Rename a file.
*
* @param Google_Service_Drive $service Drive API service instance.
* @param string $fileId ID of the file to rename.
* @param string $newTitle New title for the file.
* @return Google_Service_Drive_DriveFile The updated file. NULL is returned if
* an API error occurred.
*/
function renameFile($service, $fileId, $newTitle) {
try {
$file = new Google_Service_Drive_DriveFile();
$file->setTitle($newTitle);
$updatedFile = $service->files->patch($fileId, $file, array(
'fields' => 'title'
));
return $updatedFile;
} catch (Exception $e) {
print "An error occurred: " . $e->getMessage();
}
}
The error it gives:
PHP Fatal error: Uncaught Error: Call to undefined method Google\Service\Drive\DriveFile::setTitle() in .\GoogleDrive.php:153
Stack trace:
#0 .\test.php(22): GoogleDrive->renameFile('drive_Id_...', 'test2.txt')
#1 {main}
thrown in .\GoogleDrive.php on line 153
Fatal error: Uncaught Error: Call to undefined method Google\Service\Drive\DriveFile::setTitle() in .\GoogleDrive.php:153
Stack trace:
#0 .\test.php(22): GoogleDrive->renameFile('drive_Id_...', 'test2.txt')
#1 {main}
thrown in .\GoogleDrive.php on line 153
It was simple.
The fill code is:
/**
* Rename a file.
*
* @param Google_Service_Drive $service Drive API service instance.
* @param string $fileId ID of the file to rename.
* @param string $newTitle New title for the file.
* @return Google_Service_Drive_DriveFile The updated file. NULL is returned if
* an API error occurred.
*/
function renameFile($service, $fileId, $newTitle) {
try {
$file = new Google_Service_Drive_DriveFile();
$file->setName($newTitle);
$updatedFile = $service->files->update($fileId, $file);
return $updatedFile;
} catch (Exception $e) {
print "An error occurred: " . $e->getMessage();
}
}
The parts the you need to change are:
Change the ->patch
to ->update
and ->setTitle
to ->setName
array('fields' => 'title'));
You need to change remove this. This will give the error:
An error occurred: { "error": { "errors": [{ "domain": "global", "reason": "invalidParameter", "message": "Invalid field selection title", "locationType": "parameter", "location": "fields" }], "code": 400, "message": "Invalid field selection title" }
}
This works on both file and folder.
Google Drive V3 PHP API documentation is sparse and cryptic.
To perform the same file rename operation using that version of the API:
$file = new Google_Service_Drive_DriveFile();
$file->setName($newTitle);
$updatedFile = $service->files->update($fileId, $file, array('fields' => 'name'));
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.