0

I have been using the google drive APIs in php to upload files from my server to google drive using a cron job. It all worked fine till now.

What I have realised is that if there is a file around 30mb and when the script reaches this file it breaks. I am using a service account. Is there a limit on file upload size ?

if(is_dir($dir)) {
$objects = scandir($dir);

$files_with_sizes = array();
foreach($objects as $item) {
    if($item != "." && $item != "..") {
        $files_with_sizes[$item] = filesize($dir . '/' . $item);
    }
}
asort($files_with_sizes);
$folderId = insertFile($drive, "Backups_" . $_SERVER['HTTP_HOST'] . '_' . date("Y-m-d H:i:s") . "_" . time(), 'Folder for backup', null, "application/vnd.google-apps.folder", "")->getId();
insertPermission($drive, $folderId, "email@domain.com", "user", "reader");
foreach ($files_with_sizes as $name => $object) {
    if ($object != "." && $object != "..") {
        $fileId = insertFile($drive, $name, $name, $folderId, "text/plain", $dir . '/' . $name)->getId();
    }
}
}

function insertFile($service, $title, $description, $parentId, $mimeType, $filename) {
$file = new Google_Service_Drive_DriveFile();
$file->setTitle($title);
$file->setDescription($description);
$file->setMimeType($mimeType);

// Set the parent folder.
if ($parentId != null) {
    $parent = new Google_Service_Drive_ParentReference();
    $parent->setId($parentId);
    $file->setParents(array($parent));
}

try {
    if(is_file($filename)) {
        $data = file_get_contents($filename);

        $createdFile = $service->files->insert($file, array(
            'data' => $data,
            'mimeType' => $mimeType,
            'uploadType' => 'multipart',
        ));
    } else {
        $createdFile = $service->files->insert($file, array(
            'mimeType' => $mimeType,
        ));
    }

    // Uncomment the following line to print the File ID
    // print 'File ID: %s' % $createdFile->getId();

    return $createdFile;
} catch (Exception $e) {
    print "An error occurred: " . $e->getMessage();
}
}

function insertPermission($service, $fileId, $value, $type, $role) {
$newPermission = new Google_Service_Drive_Permission();
$newPermission->setValue($value);
$newPermission->setType($type);
$newPermission->setRole($role);
try {
    return $service->permissions->insert($fileId, $newPermission);
} catch (Exception $e) {
    print "An error occurred: " . $e->getMessage();
}
return NULL;
}
manlio
  • 18,345
  • 14
  • 76
  • 126
  • Please provide the code that is potentially broken/having issues so we can assist you in debugging, in the questions current state, we will not be able/nor want to help you. – zanderwar Sep 02 '16 at 07:52
  • I think it depends on the file type what size it can be docs I believe can be around 50 MB for example. as long as you are using Resumable upload there shouldn't be a problem. what @Zanderwar said show your code. – Linda Lawton - DaImTo Sep 02 '16 at 08:06
  • Could also be your `memory_limit` or max execution time (php.ini) – zanderwar Sep 02 '16 at 08:08
  • max execution time in php.ini is being overridden in my script with set_time_limit(0) – Kojo Nkrumah Sep 02 '16 at 08:13
  • Not to assume you're lacking the inclination to do so; but can you ensure errors are enabled (http://stackoverflow.com/questions/1053424/how-do-i-get-php-errors-to-display) and re-attempt the upload. If done via ajax use your "Network" tab in "Inspect Element" and check the response for the error (+1 for fixing your question) – zanderwar Sep 02 '16 at 08:18
  • use resumable uploads and upload in smaller chunks. – pinoyyid Sep 02 '16 at 08:33
  • @Zanderwar adding display_errors helped me find the cause and it had to do with allowed memory size being exhausted. – Kojo Nkrumah Sep 02 '16 at 08:43
  • @pinoyyid I think the smaller chunks uploads will help but can i get any help on how this is done? – Kojo Nkrumah Sep 02 '16 at 08:43
  • 1
    @KojoNkrumah [here you go](https://developers.google.com/api-client-library/java/google-api-java-client/media-upload) – zanderwar Sep 02 '16 at 08:47
  • Added a brief answer for others to easily read without looking through the comments :) – zanderwar Sep 02 '16 at 08:54

1 Answers1

0

As discussed in your comments, and in my experience this is normally solved by one of two things:

ini_set('memory_limit','125M'); // Memory limit is always a scary thing to touch, don't run out of it ;)
// --- and/or ---
set_time_limit(0) // Again, use with caution because an infinite loop could be painful: while (1) { continue; }

As @KojoNkrumah brings up, it is far more advantageous and a lot safer to use resumable uploads and upload > 10MB files in chunks

zanderwar
  • 3,440
  • 3
  • 28
  • 46