[英]List all the .csv files from a directory and its subdirectories uisng PHP
[英]Files list with an exclusion of folders and all its grand children subdirectories
我的驱动器上有以下层次结构:
-- logo1.png
|
|-- logo2.png
|
-- myFolder
| |
| --logo3.png
|
|
-- myPrivateFolder
|
--anotherPrivateFolder
|
-- logo4.png
我正在使用这个简单的脚本列出我的文件:
$client = new Google_Client();
$client->setAuthConfig($oauth_credentials);
$client->setRedirectUri($redirect_uri);
$client->addScope("https://www.googleapis.com/auth/drive");
$service = new Google_Service_Drive($client);
$result = array();
try {
$parameters = array(
'q' => "mimeType contains 'image/'",
);
$files = $service->files->listFiles($parameters);
$result = array_merge($result, $files->getFiles());
} catch (Exception $e) {
print "Une erreur s'est produite : " . $e->getMessage();
}
return $result;
作为参数,我在获取它们后为排除的文件夹ID:
$parameters = array('q' => "'not 'myFolderID' in parents and not 'myprivateFolderID' in parents");
运行脚本时,我只希望得到logo1.png
和logo2.png
,因为我排除了myFolder
和myPrivateFolder
,但是我也得到了logo4.png
因为它具有anotherPrivateFolder
并且myPrivateFolder
是祖父母。 我知道Google Drive API没有树形结构的概念,因此我做了一个递归函数,以在排除的Folders的所有子目录中传播。 在这种情况下,我的脚本将获取myFolder
和myPrivateFolder
下面的文件夹的所有ID。 这是我的功能:
$excluded_folders_id = array();
function retrieveAllChildren($service, $parameters) {
global $excluded_folders_id;
$results = array();
$pageToken = NULL;
do{
try {
if ($pageToken) {
$parameters['pageToken'] = $pageToken;
}
$files = $service->files->listFiles($parameters);
$results = array_merge($results, $files->getFiles());
$pageToken = $files->getNextPageToken();
} catch (Exception $e) {
print "Une erreur s'est produite : " . $e->getMessage();
$pageToken = NULL;
}
}while($pageToken);
foreach ($results as $result) {
echo "=> New children to exclude : ".$result->name."\n";
array_push($excluded_folders_id, $result->id);
retrieveAllChildren($service, array('q' => "'".$result->id."' in parents and mimeType = 'application/vnd.google-apps.folder'"));
}
}
retrieveAllChildren($service, array('q' => "name = 'myPrivateFolder'"))
结果,我得到一个数组,其中包含我的排除文件夹及其所有子目录的ID。 然后,我构建查询,对于每个排除的文件夹ID,我添加...和“ newsubdirectoryID”不在父目录中。 在包含数百个子文件夹的驱动器上运行请求时,出现413错误,提示我的客户端请求太大。 然后如何无限制地排除文件夹及其所有内容?
预先感谢您的任何帮助
编辑:
function isFileInExcluded($service, $entity_id){
global $excluded_folders_id;
$entityProperties = $service->files->get($entity_id,array('fields' => 'parents, id, name'));
If there is no more parents (so if on top of tree - no excluded encountered)
if($entityProperties->parents == NULL){
return true;
}
// If file has excluded folder on upper part of tree
if(in_array($entityProperties->parents[0], $excluded_folders_id) == true){
return true;
}else{
// Spread acrosse upper parents
$parentProperties = $service->files->get($entityProperties->parents[0],array('fields' => 'parents, id, name'));
isFileInExcluded($service, $parentProperties->id);
}
}
“在包含数百个子文件夹的驱动器上运行请求时,出现413错误,提示我的客户请求太大”
您将需要重新设计您的应用程序。
您可以采取以下几种方法:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.