Hello,
I want to use the “large_file_upload” code in the URL below to upload a directory containing sub-directories and files.
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?
GuzzleHttp\Psr7\Request Object
(
[method:GuzzleHttp\Psr7\Request:private] => GET
[requestTarget:GuzzleHttp\Psr7\Request:private] =>
[uri:GuzzleHttp\Psr7\Request:private] => GuzzleHttp\Psr7\Uri Object
(
[scheme:GuzzleHttp\Psr7\Uri:private] => https
[userInfo:GuzzleHttp\Psr7\Uri:private] =>
[host:GuzzleHttp\Psr7\Uri:private] => www.googleapis.com
[port:GuzzleHttp\Psr7\Uri:private] =>
[path:GuzzleHttp\Psr7\Uri:private] => /drive/v3/files
[query:GuzzleHttp\Psr7\Uri:private] => q=name%3D%27webyonetimi-2023-10-27-15-36-Tam.sql%27%20and%20%2714s4qj90-jM6X6bQPrV3jYI08lGGIAYST%27%20in%20parents
[fragment:GuzzleHttp\Psr7\Uri:private] =>
[composedComponents:GuzzleHttp\Psr7\Uri:private] =>
)
[headers:GuzzleHttp\Psr7\Request:private] => Array
(
[Host] => Array
(
[0] => www.googleapis.com
)
[X-Php-Expected-Class] => Array
(
[0] => Google\Service\Drive\FileList
)
)
[headerNames:GuzzleHttp\Psr7\Request:private] => Array
(
[host] => Host
[x-php-expected-class] => X-Php-Expected-Class
)
[protocol:GuzzleHttp\Psr7\Request:private] => 1.1
[stream:GuzzleHttp\Psr7\Request:private] =>
)
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
Google\Service\Drive\FileList Object
(
[files] => Array
(
[0] => Google\Service\Drive\DriveFile Object
(
[id] => 1fJLIo5hvexvsLppTCtDJQs11OVZ1PsSC
[name] => 2023-09-12-13-25-26.zip
)
)
[filesType:protected] => Google\Service\Drive\DriveFile
[filesDataType:protected] => array
[incompleteSearch] =>
[kind] => drive#fileList
[nextPageToken] =>
)
I would be happy if you help
`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
}
}
}
}`
Thank you from now