2012-09-20 101 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 
     } 
    }