Reputation: 695
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,
));
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.
Upvotes: 1
Views: 2120
Reputation: 13128
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();
}
And you'll notice in their Response
section:
If successful, this method returns a Files resource in the response body.
Meaning you'll get the uploaded file in return. Otherwise the catch()
exception will be thrown as you see above.
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.
Upvotes: 2