我可以上传常规文件。这里没问题。 更新现有文件时出现错误 我添加了 'uploadType' => 'multipart' ,它再次出错。
目录中有一个“
.htaccess
”文件,其内容为“deny from all
”。
我从“.htaccess”驱动器中删除它,它在另一个文件中给出错误。
错误消息示例:
Google\Service\Drive\Resource\Files->update( $fileId = '1ouvREdK6mJSOu_AqGyffcuwFvmvHM4Ar', $postBody = NULL, $optParams = 'deny from all', [] )
另一个文件中的错误显示文件内容
第二行给出错误
$content = file_get_contents($filePath); $service->files->update($existingFile->getId(), null, $content, []);
我正在使用 Google 云端硬盘服务帐户 我正在使用 php 8.2 版本库
我的英语很差。
我的完整代码如下。
加载目录及其所有内容,甚至子目录和子文件,工作顺利。
这里的问题是:
覆盖现有文件时出错,无法覆盖现有文件。
`//文件夹及子内容加载功能
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;
}
}`