peter_anderson Posted February 7, 2010 Share Posted February 7, 2010 Hello, I am trying to load the name of all .txt files within the directory, excluding one. I cannot think of the best way to approach it (if any)? Can anyone help? Thanks Link to comment https://forums.phpfreaks.com/topic/191248-list-all-txt-files-in-directory/ Share on other sites More sharing options...
xjake88x Posted February 7, 2010 Share Posted February 7, 2010 <?php //Open directory $dir = dir("my/directory"); //List files in directory while (($file = $dir->read()) !== false){ //Make sure it's a .txt file if(strlen($file) < 5 || substr($file, -4) != '.txt') continue; echo "filename: " . $file . "<br />"; } $dir->close(); ?> Link to comment https://forums.phpfreaks.com/topic/191248-list-all-txt-files-in-directory/#findComment-1008372 Share on other sites More sharing options...
$Three3 Posted February 7, 2010 Share Posted February 7, 2010 Hello, I am trying to load the name of all .txt files within the directory, excluding one. I cannot think of the best way to approach it (if any)? Can anyone help? Thanks You most likely use a for loop or a foreach loop depending if you're dealing with an array or not. You might also want to heck out the substr() function, the is_file() function, is_dir() function, scandir() function. That should get you started. Link to comment https://forums.phpfreaks.com/topic/191248-list-all-txt-files-in-directory/#findComment-1008373 Share on other sites More sharing options...
peter_anderson Posted February 7, 2010 Author Share Posted February 7, 2010 <?php //Open directory $dir = dir("my/directory"); //List files in directory while (($file = $dir->read()) !== false){ //Make sure it's a .txt file if(strlen($file) < 5 || substr($file, -4) != '.txt') continue; echo "filename: " . $file . "<br />"; } $dir->close(); ?> Thank you {resolved} Link to comment https://forums.phpfreaks.com/topic/191248-list-all-txt-files-in-directory/#findComment-1008381 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.