![](/img/trans.png)
[英]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.