...

View Full Version : readdir write names to Mysql



ajloun
02-21-2010, 10:16 PM
Hello

I use this Code to List Files in My director .. I need to inser the Name of the Files into Mysql ..

How So ..this wht i tried and did not work ..


$dir = 'images/';
if ($dh = opendir("$dir")) {
$files = array();
while (($file = readdir($dh)) !== false) {
if ($file == "." or $file == ".." or $file == "Thumbs.db" or $file == "index.php" or $file == "index.html" ){
continue; }
$files[] = $file; }
$files_num=count($files);
echo " we found".$files_num." file<br><hr>";
$counter=0;
while($counter<$files_num){
echo" $files[$counter]<br>";
$counter++;
} }

///
$sql = "INSERT INTO test_tble (title) VALUES ('$files[$counter]')";
$ret = mysql_query($sql);

if (!$sql){
echo "<center>err</center>";
}else{
echo "<center>ok</center>";
}

Killermud
02-21-2010, 10:24 PM
Well i would do it like this :


$directory='./images/';
$results = array();
$handler = opendir($directory);

while ($file = readdir($handler)) {

if ($file != '.' && $file != '..')
$results[] = $file;
}

foreach($results as $key => $val){
$sql = "INSERT INTO test_tble (title) VALUES ('$val')";
$ret = mysql_query($sql);
}

ajloun
02-21-2010, 10:49 PM
Great .. Works well ..
I kinda wanted to list the Files names too ..and be able to click Go before insert it .. i will try do it myself .. and hope i get it ..if not i will Update this Topic:)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum