0

我想遍历文件夹中的图像和缩略图并将它们插入数据库。我想使用 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, echo $images[$x]之后直接签入,它会根据需要回显所有没有目录的图像。

但是当我检查数据库中的插入时,我看到目录已添加为记录。为什么是这样?

谢谢!

4

3 回答 3

2

(Deleting old answer, as the issue was a typo)

scandir returns a list of files in a given directory. When you use is_dir, it's looking in the current directory for those files. I think what you need to do is:

if(!is_dir("./images/all_comics/" . $images[$x])) {
....
于 2012-09-20T17:30:47.463 回答
2

Your echo is executed inside if, but query does not:

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]')");
       }
}

Also, get rid of mysql_* for PDO, and consider glob as a way to browse for files excluding directories.

于 2012-09-20T17:31:36.747 回答
0

您也可以使用 glob

$files = glob('./images/all_comics/*');
    foreach ($files as $file) {
        if (!is_dir($file)) {
            //Do Insert
        }
    }
于 2012-09-20T17:50:09.920 回答