2017-07-30 40 views
0

PHP目錄列表不起作用,我做錯了什麼?電影目錄中有文件夾,但它們未顯示在我的輸出中。PHP目錄列表不起作用

<?php  
    // declare the folder  
    $ourDir = "movies"; 

    // prepare to read directory contents  
    $ourDirList = @opendir($ourDir); 

    // loop through the items  
    while ($ourItem = readdir($ourDirList))  
    {   
     // check if it is a directory  
     if (is_dir($ourItem))  
     {  
      echo $ourItem; 
      echo "<br />"; 
     } 
    } 
closedir($ourDirList); 
?> 
+0

後'

+0

我試圖檢查建議。我沒有得到任何錯誤,仍然沒有目錄列表。電影文件夾與php腳本位於相同的目錄中(電影文件夾內還有2個空文件夾)。 – dreadycarpenter

+0

檢查你'電影'文件夾的權限(需要爲077) –

回答

1

的問題是,當你查看$ ourItem是一個文件夾,你就忘記正在尋找在文件夾的當前目錄。

見下文。

// declare the folder  
$ourDir = "movies"; 

// prepare to read directory contents  
$ourDirList = @opendir($ourDir); 

// loop through the items  
while ($ourItem = readdir($ourDirList))  
{   
    // check if it is a directory 
    if (is_dir($ourDir.DIRECTORY_SEPARATOR.$ourItem))  
    {  
     echo $ourItem; 
     echo "<br />"; 
    } 
}closedir($ourDirList);