The problem here is that the first file is written without any problems, but the second file gives the error message in the title while writing.
When I print the $results variable in the search File() function to a file, it gives an error because the last array gives the result as follows. What could be the reason for this?
I think it gives an error because it does not contain "[files] => Array", [id] => 1fJLIo5hvexvsLppTCtDJQs11OVZ1PsSC, [name] => 2023-09-12-13-25-26.zip" in the array.
How can I solve this problem?
The first successful file result array is as follows
`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;
}
}
########################################################################
########################################################################
function uploadFolder($client, $service, $parentId, $folderPath) {
$folderName = basename($folderPath);
$existingFolder = searchFile($service, $parentId, $folderName);
// If the selected directory exists
if ($existingFolder) {
$createdFolder = $existingFolder;
} else { // If the selected directory does not exist, create it
$folder = new Google\Service\Drive\DriveFile();
$folder->setName($folderName);
$folder->setMimeType('application/vnd.google-apps.folder');
$folder->setParents([$parentId]);
$createdFolder = $service->files->create($folder);
}
// Simple code example
$files = scandir($folderPath);
foreach ($files as $value) {
$filePath = $folderPath . '/' . $value;
if (is_dir($filePath)) {
// If item is a folder, create it as a subfolder
uploadFolder($client, $service, $createdFolder->id, $filePath);
} else {
// If item is a file, check if it exists
$existingFile = searchFile($service, $createdFolder->id, $value);
if ($existingFile) { // If the file exists, overwrite it
// The beginning of the code "large_file_upload" in the URL
$file = new Google\Service\Drive\DriveFile();
$file->name = $value;
$chunkSizeBytes = 1 * 1024 * 1024;
// Call the API with the media upload, defer so it doesn't immediately return.
$client->setDefer(true);
$request = $service->files->update($existingFile->id, $file);
// Create a media file upload to represent our upload process.
$media = new Google\Http\MediaFileUpload(
$client,
$request,
'text/plain',
null,
true,
$chunkSizeBytes
);
$media->setFileSize(filesize($filePath));
function readVideoChunk($handle, $chunkSize)
{
$byteCount = 0;
$giantChunk = "";
while (!feof($handle)) {
// fread will never return more than 8192 bytes if the stream is read
// buffered and it does not represent a plain file
$chunk = fread($handle, 8192);
$byteCount += strlen($chunk);
$giantChunk .= $chunk;
if ($byteCount >= $chunkSize) {
return $giantChunk;
}
}
return $giantChunk;
}
// Upload the various chunks. $status will be false until the process is
// complete.
$status = false;
$handle = fopen($filePath, "rb");
while (!$status && !feof($handle)) {
// read until you get $chunkSizeBytes from $filePath
// fread will never return more than 8192 bytes if the stream is read
// buffered and it does not represent a plain file
// An example of a read buffered file is when reading from a URL
$chunk = readVideoChunk($handle, $chunkSizeBytes);
$status = $media->nextChunk($chunk);
}
// The final value of $status will be the data from the API for the object
// that has been uploaded.
$result = false;
if ($status != false) {
$result = $status;
}
fclose($handle);
// End of code "large_file_upload" in URL
}else{ // If the file does not exist, create a new one
}
}
}
}`
Hello,
I want to use the "large_file_upload" code in the URL below to upload a directory containing sub-directories and files. https://github.com/googleapis/google-api-php-client/blob/e70273c06d18824de77e114247ae3102f8aec64d/examples/large-file-upload.php#L4
The problem here is that the first file is written without any problems, but the second file gives the error message in the title while writing.
When I print the $results variable in the search File() function to a file, it gives an error because the last array gives the result as follows. What could be the reason for this?
I think it gives an error because it does not contain "[files] => Array", [id] => 1fJLIo5hvexvsLppTCtDJQs11OVZ1PsSC, [name] => 2023-09-12-13-25-26.zip" in the array.
How can I solve this problem?
The first successful file result array is as follows
I would be happy if you help
Thank you from now