2012-09-20 188 views
0

我想循環瀏覽文件夾中的圖像和縮略圖,並將它們插入到數據庫中。 我想用is_dir過濾掉目錄。is_dir不適用於循環

我:

$images = scandir('./images/all_comics/'); 
$thumbs = scandir('./images/thumbnails/'); 

for($x=0; $x<count($images); $x++) 
    { 
    if(!is_dir($images[$x])) 
     { 
     //This shows all images WITHOUT directories 
     echo $images[$x]; 

     //This is STILL adding images AND directories to database 
     mysql_query("INSERT INTO images (imgpath, thumbpath) VALUES ('$images[$x]', '$thumbs[$x]')"); 
     } 
    } 

我在那裏有一個支票直接is_dir後,回聲$圖像[$ X],其回聲的所有圖像,而無需目錄,按照需要!

但是,當我檢查數據庫中的插入,我看到目錄已添加爲記錄。爲什麼是這樣?

謝謝!

+0

使用'glob' - '$圖像=水珠( './圖像/ all_comics/* JPG');',相應地更改格式。 – moonwave99

回答

2

(刪除舊的答案,因爲這個問題是一個錯字)

scandir返回給定目錄的文件列表。當您使用is_dir時,它正在當前目錄中查找這些文件。我認爲你需要做的是:

if(!is_dir("./images/all_comics/" . $images[$x])) { 
.... 
+0

好的,那是我在打字時犯的一個小錯誤。即使在修復之後,它仍然在做同樣的事情。 – Growler

+0

所以echo語句不打印任何目錄,但仍然添加它們? – andrewsi

+0

這是正確的。 – Growler

2

echo在裏面if執行,但查詢並不:

for($x=0; $x<count($images); $x++) 
{ 
     if(!is_dir($images[$x])) 
     { 
     echo $images[$x]; //This shows all images WITHOUT directories 
     mysql_query("INSERT INTO images (imgpath, thumbpath) VALUES ('$images[$x]', '$thumbs[$x]')"); 
     } 
} 

而且,擺脫mysql_*PDO,並考慮glob的一種方式瀏覽除目錄以外的文件。

0

您還可以使用水珠

$files = glob('./images/all_comics/*'); 
    foreach ($files as $file) { 
     if (!is_dir($file)) { 
      //Do Insert 
     } 
    }