将大文件上载到google drive(php google drive api)
我可以使用以下代码将文件上载到google drive(大小<100 mb)将大文件上载到google drive(php google drive api),php,memory-management,upload,google-drive-api,chunked-encoding,Php,Memory Management,Upload,Google Drive Api,Chunked Encoding,我可以使用以下代码将文件上载到google drive(大小
但对于较大的文件,显示此错误:
Fatal error: Out of memory (allocated 2359296) (tried to allocate 300457543 bytes) in /home/bahmanx2/public_html/gd/index.php on line 40
我的代码是
<?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);
?>
如何解决这个错误?
我可以使用chuncked或curl将文件下载到我的服务器上,但如何将大文件上传到google drive?你必须像这样将文件分块
$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);
事实上,提出的“无处不在”并不是解决这个问题的好办法。还有更简单的-在调用$this->\u service->files->insert的库中,有一个未记录的选项,不将文件内容传输到“data”中,而是将文件的完整路径粘贴到“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'];
php的版本是什么,运行的机器是什么,os/memory/webserver是什么?看起来当您试图分配~300mb内存时,它限制您将内存分配为2.25mb。我的cpanel php版本是5.3。php.ini中的内存限制大小为128MB。我可以上传大小低于100MB的文件。我搜索谷歌并使用ini_设置,我编写了顶级代码,但没有解决问题。请注意,这仅适用于php 2.0版的google API SDK,目前仍在编写测试版。请注意,这仅适用于谷歌PHP版本2.0的API SDK,目前仍处于测试状态