Google 驱动 php 上传中断
Google drive php uploads breaks
我一直在使用 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;
}
正如您在评论中所讨论的,根据我的经验,这通常可以通过以下两种方式之一解决:
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
文件
更有优势也更安全
我一直在使用 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;
}
正如您在评论中所讨论的,根据我的经验,这通常可以通过以下两种方式之一解决:
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
文件