2017-11-04 21 views
-1

我有一個腳本可以掃描頂層目錄,並返回其內容(它們都是目錄本身)=>$scan = scandir("$path");。我返回的嵌套目錄不斷由外部Python腳本創建。使用mtime <10分鐘前從數組中刪除所有目錄

我希望能夠排除上次修改少於10分鐘前的子目錄。我不想從內存中刪除目錄(即我不想使用rmdir()),但我想要做與Python的list.remove()函數基本相同的功能。我所希望的是能夠從數組中刪除這些元素。

有沒有一個函數可以做到這一點?

到目前爲止,我一直在搜索的所有內容都給了我一種從服務器上刪除目錄的方法。


編輯:下面是腳本的例子:

ls.php

$path = $_GET['path']; 
$regex=$_GET["regex"]; // just a string passed in which selects directories matching a naming convention 

$scan = scandir($path); 

foreach ($scan as $child) { 
    if (is_dir("$path/$child")) { 
     if (preg_match("/$regex/",$child)) { 
      if (!preg_match("/^\./",$child)) { 
       $nested[]=$child; 
      } 
     } 
    } 
} 

當前腳本我真的很簡單。返回的內容是所有目錄(不包括./和../),我只想知道如何刪除數組中的元素,這些元素是10分鐘前修改的目錄。

+0

讓我們來看看一個數組或腳本,你使用的是創建數組的一個例子。 – Rasclatt

+0

@Rasclatt的帖子已被編輯 – nat5142

回答

0

您可能需要使用filemtime()strtotime()弄明白,像:

# Assign now 
$now = strtotime('now'); 
# Loop scanned directory 
foreach ($scan as $child) { 
    # Assign dir name 
    $dir = $path.'/'.$child; 
    # Not a directory, skip 
    if(!is_dir($dir)) 
     continue; 
    # Not match, skip 
    if(!preg_match("/$regex/",$child)) 
     continue; 
    # If dots, skip 
    if(preg_match("/^\./",$child)) 
     continue; 
    # Take now minus the last modified time of the file 
    $diff = ($now - filemtime($dir)); 
    # See if the difference is more than 600 seconds (10 minutes) 
    if($diff > 600) 
     continue; 
    # Assign if less than 10 minutes 
    $nested[] = $child; 
} 
+0

哦,我甚至沒有考慮過最初排除這些文件。好想法!我會很快實現這一點。非常感謝! – nat5142