我一直在使用php中的google驱动器API,使用cron作业将文件从我的服务器上传到google驱动器。这一切都很好,直到现在。
我所知道的是,如果有一个大约30mb的文件,当脚本到达这个文件时它就会中断。我正在使用服务帐户。文件上传大小是否有限制?
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;
}
答案 0 :(得分:0)
正如您的评论中所讨论的那样,根据我的经验,这通常可以通过以下两种方法之一来解决:
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; }
正如@KojoNkrumah提出的那样,使用resumable uploads并以块的形式上传> 10MB
文件会更有利,也更安全