2010-11-15 157 views
2

我想檢查一下我的服務器上的文件夾是否有圖像。我有這個小函數在PHP中,但不工作,我不知道爲什麼:PHP - 檢查一個文件夾中是否存在文件

$path = 'folder/'.$id; 
function check($path) { 
    if ($handle = opendir($path)) { 
     $array = array(); 
     while (false !== ($file = readdir($handle))) { 
      if ($file != "." && $file != ".." && count > 2) { 
       echo "folder not empty"; 
      } else { 
       echo "folder empty"; 
      } 
     } 
    } 
    closedir($handle); 
} 

任何幫助將不勝感激,在此先感謝。

+1

什麼是「count」變量(我假設)?它有沒有改變? – 2010-11-15 19:04:48

回答

3

它不工作,因爲count從無處到來。試試這個:

$path = 'folder/'.$id; 
function check($path) { 
    $files = glob($path.'/*'); 
    echo empty($files) ? "$path is empty" : "$path is not empty"; 
} 
+0

Vars out of quotes ...可以調試你的大部分代碼;)http://www.pfz.nl/wiki/variabelen-buiten-quotes/(荷蘭語) – dododedodonl 2010-11-15 19:31:35

+1

...那是什麼? – netcoder 2010-11-15 19:33:57

+0

只是試圖翻譯它與谷歌翻譯...這是一篇文章,解釋(壞)的PHP行爲:'「$路徑爲空」' – dododedodonl 2010-11-15 20:03:33

0

試試這個:

$path = 'folder/'.$id; 
function check($path) { 
    if (is_dir($path)) { 
     $contents = scandir($path); 
     if(count($contents) > 2) { 
      echo "folder not empty"; 
     } else { 
      echo "folder empty"; 
     } 
    } 
    closedir($handle); 
} 

它計算路徑的內容。如果有兩個以上的項目,那麼它不是空的。我們忽略的兩項是「。」和「..」。

相關問題