Question

[EDIT] As the error specifies, my php.ini allocates 1GB of RAM per script (memory_limit = 1G), which should be more than enough, so I'm not asking how to increase or modify this limit. I just found out this question can be considered a duplicate of this other unanswered question: Upload large file to google drive with PHP Client Library

I'm trying to upload a big file (237MB) to my google drive account from my VPS using the PHP CLI script provided on google API's quickstart guide and I get the following error:

PHP Fatal error:  Allowed memory size of 1073741824 bytes exhausted 
(tried to allocate 316952783 bytes) in 
/var/www/google-api-php-client/src/service/Google_MediaFileUpload.php on line 135

The script:

<?php
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();
// Get your credentials from the APIs Console
$client->setClientId('MY_CLIENT_ID');
$client->setClientSecret('MY_CLIENT_SECRET');
$client->setRedirectUri('urn:ietf:wg:oauth:2.0:oob');
$client->setScopes(array('https://www.googleapis.com/auth/drive'));

$service = new Google_DriveService($client);

$authUrl = $client->createAuthUrl();

//Request authorization
print "Please visit:\n$authUrl\n\n";
print "Please enter the auth code:\n";
$authCode = trim(fgets(STDIN));

// Exchange authorization code for access token
$accessToken = $client->authenticate($authCode);
$client->setAccessToken($accessToken);

//Insert a file
$file = new Google_DriveFile();
$file->setTitle('My zip file');
$file->setDescription('A test zip file');
$file->setMimeType('application/zip');

$data = file_get_contents('myfile.zip');

$createdFile = $service->files->insert($file, array(
      'data' => $data,
      'mimeType' => 'application/zip',
    ));

print_r($createdFile);
?>

top says I've got 787816k physical memory free (plus 2G cache), so I don't get where the problem is. Any clues?

Was it helpful?

Solution

[EDIT] This is a valid answer to the memory allocation problem, but does not solve file upload within reasonable script memory usage constrains, which was the actual question. For an answer to that question, check out https://stackoverflow.com/a/14693917/1060686

PHP has a mechanism that prevents PHP scripts from using more memory than allowed by a given threshold. This threshold can be configured in the php.ini (for all scripts) or per script. I would advice to set it per script to a higher value.

On top of your script add the following line:

ini_set('memory_limit', '500M');

Explanation of large memory usage: You read the file using the following line:

$data = file_get_contents('myfile.zip');

This requires PHP to read the full file contents into memory ( in var $data ). It would be better to read the file blockwise e.g. in 4096 byte blocks and send them to network immediately. Like I said, I currently don't know if its possible using the google API that you are using.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top