问题描述
我正在使用PHP sdk.我使用以下代码将文件插入驱动器.
I am using the PHP sdk. I use the below code to insert files into drive.
$file = new Google_DriveFile();
$file->setTitle($title);
$file->setDescription($description);
$file->setMimeType($mimeType);
if ($parentId != null) {
$parent = new Google_ParentReference();
$parent->setId($parentId);
$file->setParents(array($parent));
}
$data = file_get_contents($uplodedFile['file']['tmp_name']);
$createdFile = $service->files->insert($file, array(
'data' => $data,
'mimeType' => $mimeType,
));
我想做的就是知道文件是否成功上传到服务器.该文件可能很大,因此一旦完成,我想收到通知.有没有办法用PHP sdk做到这一点?我无法使用实时API.
What I want to do is to know if the file is uploaded successfully to the server. This file may be very large so once it's completed, I want to have notification. Is there a way to do this with PHP sdk? I cannot use real time API.
推荐答案
如果您阅读了他们的 API文档(插入),您可以看到它们将上传的文件包装在try/catch
块中.
If you read their API Documentation (insert), you can see they wrap their upload in a try/catch
block.
try {
$data = file_get_contents($filename);
$createdFile = $service->files->insert($file, array(
'data' => $data,
'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();
}
您会在他们的Response
部分中注意到:
And you'll notice in their Response
section:
这意味着您将获得上传的文件作为回报.否则,将如上所示抛出catch()
异常.
Meaning you'll get the uploaded file in return. Otherwise the catch()
exception will be thrown as you see above.
此外,如果向下滚动,您会注意到它们具有可使用的PHP库,并且在其功能中指出:
Also, you'll notice if you scroll down, they have a PHP Library you could use, and in their function they state:
@return Google_DriveFile The file that was inserted. NULL is returned if an API error occurred.
这篇关于有没有办法检查文件是否已成功使用Google Drive API上传?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!