Page 1 of 1

Reading file names

Posted: Fri Nov 09, 2007 8:26 am
by aceconcepts
Hi,

I have a directory full of images and I need to extract each file name and insert them into a db table.

Does anyone know of a way to read a directory of files and extract file names?

Posted: Fri Nov 09, 2007 8:36 am
by seppo0010

Code: Select all

$path = './';
$dir = opendir($path);
while ($file = readdir($dir)) {
	if ($file === '.' || $file === '..' || !is_file($file)) continue;
	echo pathinfo($file, PATHINFO_BASENAME) , "\n";
}

Posted: Fri Nov 09, 2007 9:37 am
by feyd

Posted: Fri Nov 09, 2007 12:34 pm
by aceconcepts
I was able to use this piece of code and it works perfectly.

Code: Select all

$image_file_path = '/home/dp/public_html/dev/velvet/images/products/thumbs/';
if ($handle = opendir($image_file_path)) {
    #echo "Directory handle: $handle\n";
   # echo "Files:\n";

    /* This is the correct way to loop over the directory. */
    while (false !== ($file = readdir($handle)))
	{
       	echo "$file\n<br />";
    }

    /* This is the WRONG way to loop over the directory. */
    /*while ($file = readdir($handle)) {
        echo "$file\n";
    }*/

    closedir($handle);
}
Thanks for the responses.