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?
Reading file names
Moderator: General Moderators
- aceconcepts
- DevNet Resident
- Posts: 1424
- Joined: Mon Feb 06, 2006 11:26 am
- Location: London
- seppo0010
- Forum Commoner
- Posts: 47
- Joined: Wed Oct 24, 2007 4:13 pm
- Location: Buenos Aires, Argentina
Code: Select all
$path = './';
$dir = opendir($path);
while ($file = readdir($dir)) {
if ($file === '.' || $file === '..' || !is_file($file)) continue;
echo pathinfo($file, PATHINFO_BASENAME) , "\n";
}- aceconcepts
- DevNet Resident
- Posts: 1424
- Joined: Mon Feb 06, 2006 11:26 am
- Location: London
I was able to use this piece of code and it works perfectly.
Thanks for the responses.
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);
}