Reputation: 2403
I need to loop through a .png image directory and insert the file name in a mysql database.
This is what I have so far:
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."'");
Upvotes: 0
Views: 568
Reputation: 14467
Another way of doing it is with glob which allows you to select only the png images if other files are also present in the same directory.
foreach (glob("*.txt") as $filename) {
$player_name = urldecode(str_replace("_HG.png", "", $filename));
mysql_query("INSERT IGNORE INTO signatures SET gamertag = '".$player_name."'");
}
Upvotes: 2
Reputation: 10442
Use opendir to open a directory and readdir to loop through it:
<?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);
}
}
?>
Upvotes: 0