我正在使用以下函数插入文件:
function insertFile($service, $title, $description, $parentId, $mimeType, $filename) {
$file = new DriveFile();
$file->setTitle($title);
$file->setDescription($description);
$file->setMimeType($mimeType);
if ($file->parentId != null) {
$parentsCollectionData = new DriveFileParentsCollection();
$parentsCollectionData->setId($file->parentId);
$file->setParentsCollection(array($parentsCollectionData));
}
try {
$datafile = file_get_contents($filename);
$createdFile = $service->files->insert($file, array(
'data' => $datafile,
'mimeType' => $mimeType,
));
print_r($datafile);
return $createdFile;
} catch (Exception $e) {
print "An error occurred: " . $e->getMessage();
}}
该函数上传文件,但没有标题。它会在我的 Google Drive 中创建一个“无标题”文件。我正在使用 API v2。
打印_r($文件):
DriveFile Object
(
[mimeType] => text/plain
[thumbnailLink] =>
[__labelsType:protected] => DriveFileLabels
[__labelsDataType:protected] =>
[labels] =>
[__indexableTextType:protected] => DriveFileIndexableText
[__indexableTextDataType:protected] =>
[indexableText] =>
[etag] =>
[lastModifyingUserName] =>
[writersCanShare] =>
[id] =>
[title] => test.txt
[ownerNames] =>
[sharedWithMeDate] =>
[lastViewedByMeDate] =>
[__parentsType:protected] => ParentReference
[__parentsDataType:protected] => array
[parents] =>
[exportLinks] =>
[originalFilename] =>
[description] => Description
[editable] =>
[kind] =>
[quotaBytesUsed] =>
[fileSize] =>
[createdDate] =>
[md5Checksum] =>
[embedLink] =>
[alternateLink] =>
[permissionsLink] =>
[modifiedByMeDate] =>
[downloadUrl] =>
[__userPermissionType:protected] => Permission
[__userPermissionDataType:protected] =>
[userPermission] =>
[fileExtension] =>
[selfLink] =>
[modifiedDate] =>
)
print_r($createdFile):
Array
(
[kind] => drive#file
[id] => 0B7fGO53dEJzbUVJwVEFRc09PMkk
[etag] => "ia2FSHMEjvcFQvtI43H5NSXK_wk/MTM0MTI1MjU5MzA5Nw"
[selfLink] => https://www.googleapis.com/drive/v2/files/0B7fGO53dEJzbUVJwVEFRc09PMkk
[alternateLink] => https://docs.google.com/file/d/0B7fGO53dEJzbUVJwVEFRc09PMkk/edit
[permissionsLink] => https://www.googleapis.com/drive/v2/files/0B7fGO53dEJzbUVJwVEFRc09PMkk/permissions
[title] => Untitled
[mimeType] => multipart/related; boundary=1411182904
[labels] => Array
(
[starred] =>
[hidden] =>
[trashed] =>
[restricted] =>
[viewed] => 1
)
[createdDate] => 2012-07-02T18:09:53.402Z
[modifiedDate] => 2012-07-02T18:09:53.097Z
[modifiedByMeDate] => 2012-07-02T18:09:53.097Z
[lastViewedByMeDate] => 2012-07-02T18:09:53.097Z
[parents] => Array
(
[0] => Array
(
[kind] => drive#parentReference
[id] => 0ALfGO53dEJzbUk9PVA
[selfLink] => https://www.googleapis.com/drive/v2/files/0B7fGO53dEJzbUVJwVEFRc09PMkk/parents/0ALfGO53dEJzbUk9PVA
[parentLink] => https://www.googleapis.com/drive/v2/files/0ALfGO53dEJzbUk9PVA
[isRoot] => 1
)
)
[downloadUrl] => https://doc-14-4c-docs.googleusercontent.com/docs/securesc/6eprdcqtdmlpnd2gmvdr6jlnlagf8s6i/9fqrnka0tci3gs14tff8ur996h1b74ip/1341252000000/15694289178108491958/15694289178108491958/0B7fGO53dEJzbUVJwVEFRc09PMkk?h=16653014193614665626&e=download&gd=true
[userPermission] => Array
(
[kind] => drive#permission
[etag] => "ia2FSHMEjvcFQvtI43H5NSXK_wk/9STkNeCmz61YXorH3hoJimnEgfM"
[id] => current
[role] => owner
[type] => user
)
[originalFilename] => Untitled
[fileExtension] =>
[md5Checksum] => 3e7959340ef7f8a8d24e5b9ad3daafe8
[fileSize] => 407
[quotaBytesUsed] => 407
[ownerNames] => Array
(
[0] => Myszczuk
)
[lastModifyingUserName] => Myszczuk
[editable] => 1
[writersCanShare] => 1
)