Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fatal error: Uncaught Error: Call to undefined method GuzzleHttp\Psr7\Request::getFiles() in #2549

Open
Ademgenc53 opened this issue Jan 7, 2024 · 0 comments
Labels
api: drive Issues related to the Drive API API.

Comments

@Ademgenc53
Copy link

Ademgenc53 commented Jan 7, 2024

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

@saranshdhingra saranshdhingra added the api: drive Issues related to the Drive API API. label Mar 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api: drive Issues related to the Drive API API.
Projects
None yet
Development

No branches or pull requests

2 participants