1

I got

failed to open stream: HTTP request failed! HTTP/1.1 404 Not Found

in php when got file from Google Drive through Google Drive API.

My code below:

public function actionGetAllImagesFromDrive($fid, $path)
    {
        $client = $this->getGoogleClient();
        $service = new \Google_Service_Drive($client);

        $optParams = array(
            'q' => "'$fid' in parents and trashed=false",
            'fields' => '*'
        );
        $results = $service->files->listFiles($optParams);

        $folderMimeType = 'application/vnd.google-apps.folder';

        if (count($results->getFiles()) != 0) {
            if (!is_dir($path)) {
                mkdir($path, 0775, true);
            }
            foreach ($results->getFiles() as $file) {
                if($file->getMimeType() == $folderMimeType) {
                    $this->actionGetAllImagesFromDrive($file->getId(), $path .'/'.$file->getName());
                } else {
                    $url = $file->getWebContentLink();

                    $file_name = $path .'/'. $file->getName();
                    print_r("Downloading: ".$file->getName()."\n");
                    file_put_contents( $file_name, file_get_contents($url));
                }
            }

        } else {
            print_r("Folder is empty.\n");
        }
    }

This code will get all image from folder and sub folder on Google Drive.

How to solve this issue?

Prisoner
  • 49,922
  • 7
  • 53
  • 105
ravenly
  • 46
  • 5

1 Answers1

0

Have you considered following the example in the library large file download

// Determine the file's size and ID
$fileId = $files[0]->id;
$fileSize = intval($files[0]->size);

// Get the authorized Guzzle HTTP client
$http = $client->authorize();

// Open a file for writing
$fp = fopen('Big File (downloaded)', 'w');

// Download in 1 MB chunks
$chunkSizeBytes = 1 * 1024 * 1024;
$chunkStart = 0;

// Iterate over each chunk and write it to our file
while ($chunkStart < $fileSize) {
  $chunkEnd = $chunkStart + $chunkSizeBytes;
  $response = $http->request(
    'GET',
    sprintf('/drive/v3/files/%s', $fileId),
    [
      'query' => ['alt' => 'media'],
      'headers' => [
        'Range' => sprintf('bytes=%s-%s', $chunkStart, $chunkEnd)
      ]
    ]
  );
  $chunkStart = $chunkEnd + 1;
  fwrite($fp, $response->getBody()->getContents());
}
Linda Lawton - DaImTo
  • 106,405
  • 32
  • 180
  • 449