循环通过图像目录

时间:2011-03-17 21:46:48

标签: php

我需要遍历.png图像目录并将文件名插入mysql数据库。

这是我到目前为止所做的:

mysql_connect('localhost', 'admin', '');
mysql_select_db('database');

// Each file is formated like this
$file = 'Abe+Froman+SK_HG.png';

$player_name = urldecode(str_replace("_HG.png", "", $file));
//echo $player_name;

mysql_query("INSERT IGNORE INTO signatures SET gamertag = '".$player_name."'");

2 个答案:

答案 0 :(得分:2)

另一种方法是使用glob,如果其他文件也出现在同一目录中,则只允许选择png图像。

foreach (glob("*.txt") as $filename) {
    $player_name = urldecode(str_replace("_HG.png", "", $filename));
    mysql_query("INSERT IGNORE INTO signatures SET gamertag = '".$player_name."'");
}

答案 1 :(得分:0)

使用opendir打开目录,然后readdir循环播放目录:

<?php
// From the manual entry for opendir
$dir = "/etc/php5/";

// Open a known directory, and proceed to read its contents
if (is_dir($dir)) {
    if ($dh = opendir($dir)) {
        while (($file = readdir($dh)) !== false) {
            //Do your sql insert - the filename is in the $file variable
        }
        closedir($dh);
    }
}
?>