簡體   English   中英

在php中使用scandir時如何排除某個文件夾

[英]how to exclude a certain folder when using scandir in php

我正在使用 scandir 列出目錄中的所有文件。 但是./../tmp文件夾應該有一個例外。

我已經有了這個來排除點和雙點:

$files = preg_grep('/^([^.])/', scandir($dir));

如何將tmp文件夾添加到其中? (文件夾名稱為 tmp)

嘗試 :

   $toRemove = array('.','..','tmp'); 

   $cdir = scandir($dir);
   
   $result = array_diff($cdir, $toRemove);

它比preg_grep更容易

我會選擇這個解決方案,因為@duskwuff 已經提到過,您當前的代碼排除了所有以.

$files = array_diff( scandir($dir), array(".", "..", "tmp") );

由於它是一個正則表達式,您可以嘗試查看負面預測

$files = preg_grep('/^(?!tmp|\.{1,2})$/', scandir($dir));

如果你想堅持使用正則表達式,我會做類似的事情

$files = preg_grep('/^(?!tmp|(?!([^.]))).*/', scandir($dir));

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM