0

I can upload files to google drive with this code that( size < 100 mb)
but for larger files show this error:

  Fatal error: Out of memory (allocated 2359296) (tried to allocate 300457543 bytes) in /home/bahmanx2/public_html/gd/index.php on line 40

my code is

<?php
ini_set('memory_limit', '-1');
ini_set('upload_max_filesize', '1000M');
ini_set('post_max_size', '1000M');
ini_set('max_input_time', 3000);
ini_set('max_execution_time', 3000);

require_once 'google-api-php-client/src/Google_Client.php';
require_once 'google-api-php-client/src/contrib/Google_DriveService.php';
$client = new Google_Client();
$client->setClientId('Client Id');
$client->setClientSecret('Client Secret');
$client->setRedirectUri('Redirect Uri');
$client->setScopes(array('https://www.googleapis.com/auth/drive'));
$service = new Google_DriveService($client);

if (isset($_GET['logout'])) { // logout: destroy token
    unset($_SESSION['token']);
    die('Logged out.');
}
if (isset($_GET['code'])) { // we received the positive auth callback, get the token and store it in session
    $client->authenticate();
    $_SESSION['token'] = $client->getAccessToken();
}
if (isset($_SESSION['token'])) { // extract token from session and configure client
    $token = $_SESSION['token'];
    $client->setAccessToken($token);
}
if (!$client->getAccessToken()) { // auth call to google
    $authUrl = $client->createAuthUrl();
    header("Location: ".$authUrl);
    die;
}
//Insert a file
$file = new Google_DriveFile();
$file->setTitle('My file');
$file->setDescription('A test file');
$file->setMimeType('application/x-rar-compressed');

$data = file_get_contents('file.rar');  // 287 MB

$createdFile = $service->files->insert($file, array(
      'data' => $data,
      'mimeType' => 'text/plain',
    ));
print_r($createdFile);
?>

how to solve this error?
I can use chuncked or curl to download files to my server but how to upload large files to google drive?

user2511140
  • 1,658
  • 3
  • 26
  • 32
  • What is the version of php, what is the machine you run under and what about os/memory/webserver? Looks like it limits you to allocate memory at 2.25mb while you tried to allocate ~300mb. – Wiggler Jtag Nov 04 '13 at 08:20
  • my cpanel php ver is 5.3. memory limit size in php.ini is 128MB.I can upload file that size is below 100MB.I search google and use ini_set that i write top of code but problem not solved – user2511140 Nov 04 '13 at 08:29

2 Answers2

2

You have to chunk your file like that

                    $file = new Google_Service_Drive_DriveFile();
                    $file->title = "title";


                    $chunkSizeBytes = 1 * 1024 * 1024;

                    // Call the API with the media upload, defer so it doesn't immediately return.
                    $client->setDefer(true);
                    $request = $service->files->insert($file);

                    // Create a media file upload to represent our upload process.
                    $media = new Google_Http_MediaFileUpload(
                      $client,
                      $request,
                      mime_content_type($filePath),
                      null,
                      true,
                      $chunkSizeBytes
                    );
                    $media->setFileSize(exec('stat -c %s "'.$filePath.'"'));
                    // Upload the various chunks. $status will be false until the process is
                    // complete.
                    $status = false;
                    $handle = fopen($filePath, "rb");
                    while (!$status && !feof($handle)) {
                      $chunk = fread($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);
                    // Reset to the client to execute requests immediately in the future.
                    $client->setDefer(false);
Pierre-Luc Bolduc
  • 485
  • 1
  • 5
  • 18
  • Only remark that this is only available with Google API SDK for PHP version 2.0 that is still at the moment of writing this in Beta state – vicenteherrera Feb 18 '19 at 21:05
0

Actually everywhere is proposed is not a very good solution to this problem. There are more simple - in the library in call to $this->_service->files->insert there is an undocumented option not to transfer the contents of the file into 'data', but paste the full path to the file in 'file':

$file = new Google_DriveFile();
$file->setMimeType('application/vnd.google-apps.file');
$file->setTitle($name);
$file->setDescription($description);

$file1 = new Google_DriveFile();
$file1->setTitle($name);


$createdFile = $this->_service->files->insert($file, array(
        'file'     => $path_to_file,
        'mimeType' => $mime,
));

$t = $this->_service->files->update($createdFile['id'], $file1);

return $createdFile['id'];
  • Only remark that this is only available with Google API SDK for PHP version 2.0 that is still at the moment of writing this in Beta state – vicenteherrera Feb 18 '19 at 21:05