Thursday, November 7, 2013

How to read folder names/ file names in PHP

1. How to read folder name in PHP

$folderpath = "./FOLDER-NAME/*";

foreach(glob($folderpath, GLOB_ONLYDIR) as $foldername){

/* Do your magic here */

echo $foldername;

}
Don't forget to add '/*' after your folder name
2. How to read file names in a folder in PHP

$folderpath = "./FOLDER-NAME/*.*";

foreach(glob($folderpath) as $filename){

/* Do your magic here */

echo $filename;

}
Don't forget to add '/*.*' after your folder name

Might you will complain that it is reading the whole path, not the folder/ filename you want to get at the end. So follow this trick. :)

$folderpath = "./FOLDER-NAME/*";

foreach(glob($folderpath, GLOB_ONLYDIR) as $foldername){

$newFolderName = str_replace("./FOLDER-NAME/", '', $foldername);

echo $newFolderName;

}
TaDa !!!!

No comments:

Post a Comment