Whats the most efficient way of turning the filenames in a directory into an array of strings?
Thanks.
Whats the most efficient way of turning the filenames in a directory into an array of strings?
Thanks.
Try using glob() - link to PHP docs
It acts the same way as your typical dir
function. If you want everything from the current directory, use glob('*')
, since glob
supports wildcard matching. If you want to see, say, text files from another directory, use glob('another/directory/*.txt')
. It's a powerful tool.
Hello!
I think the one way would probably be just reading out the directory with a loop and storing each element in your array:
$files = array ( );
$dirHandle = opendir('.');
while ( $currentFile = readdir($dirHandle) )
{
if ( $currentFile == '.' or $currentFile == '..' )
{
continue;
}
$files[] = $currentFile;
}
closedir($dirHandle);
best regards, lamas
// path
$mydir = '.';
$files = array();
$dir = opendir($mydir);
while(($myfile = readdir($dir)) !== false)
{
if($myfile != '.' && $myfile != '..' && is_file($myfile) )
{
$files[] = $myfile;
}
}
closedir($dir);
print_r($files);
Either by using glob()
:
$files = glob('/path/to/dir/*');
Or scandir()
:
$files = array_diff(scandir('/path/to/dir/'), array('.', '..'));
@Bill Karwin - scandir()
without array_diff()
:
$files = scandir('.');
$result = array();
foreach ($files as $file)
{
if (($file == '.') || ($file == '..'))
{
continue;
}
$result[] = $file;
}
// slightly modified from markb's answer
// poster said "filenames" not files and directories.
// glob has no GLOB_ONLYFILES
// path
$mydir = '.';
$files = array();
$dir = opendir($mydir);
while(($myfile = readdir($dir)) !== false)
{
if( !is_dir($myfile) ) // is_dir will match . and ..
{
$files[] = $myfile;
}
}
closedir($dir);
print_r($files);
With glob :
// path
$mydir = '.';
$files = array();
foreach(glob($mydir) as $file_or_dir) {
if( !is_dir($myfile) ) // is_dir will match . and ..
{
$files[] = $myfile;
}
}
print_r($files);
The solution using the fewest lines of code isn't always the most efficient if by efficient you mean fastest.
I tested the solutions given by some other answers in this thread in my /usr/lib directory, which contains 394 files. I ran each test 1000 times.
edit: I ran new tests after reading @Alix's comments, and after @lamas's solution changed.
Below is the script I used to test, so you can try it yourself:
<?php
$n = 1000;
$start = microtime(true);
$files = array();
for ($i = 0; $i < $n; ++$i) {
foreach(glob('*') as $file_or_dir) {
if( !is_dir($file_or_dir) ) // is_dir will match . and ..
{
$files[] = $file_or_dir;
}
}
}
$end = microtime(true);
echo "Time for @Kagee's solution: foreach(glob()) = " . ($end-$start) . "\n";
$start = microtime(true);
for ($i = 0; $i < $n; ++$i) {
$files = glob('*');
}
$end = microtime(true);
echo "Time for @Matchu's solution: glob() = " . ($end-$start) . "\n";
$start = microtime(true);
$files = array();
for ($i = 0; $i < $n; ++$i) {
foreach(glob('*', GLOB_NOSORT) as $file_or_dir) {
if( !is_dir($file_or_dir) ) // is_dir will match . and ..
{
$files[] = $file_or_dir;
}
}
}
$end = microtime(true);
echo "Time for @Kagee's solution: foreach(glob(GLOB_NOSORT)) = " . ($end-$start) . "\n";
$start = microtime(true);
for ($i = 0; $i < $n; ++$i) {
$files = scandir('.');
$result = array();
foreach ($files as $file)
{
if (($file == '.') || ($file == '..'))
{
continue;
}
$result[] = $file;
}
}
$end = microtime(true);
echo "Time for @Alix Axel's solution: scandir() = " . ($end-$start) . "\n";
$start = microtime(true);
for ($i = 0; $i < $n; ++$i) {
$files = array_diff(scandir('.'), array('.', '..'));
}
$end = microtime(true);
echo "Time for @Alix Axel's solution: array_diff(scandir()) = " . ($end-$start) . "\n";
$start = microtime(true);
for ($i = 0; $i < $n; ++$i) {
$files = array();
$dir = opendir('.');
while(($myfile = readdir($dir)) !== false)
{
if( !is_dir($myfile) )
{
$files[] = $myfile;
}
}
closedir($dir);
}
$end = microtime(true);
echo "Time for @Kagee's solution: readdir() = " . ($end-$start) . "\n";
$start = microtime(true);
for ($i = 0; $i < $n; ++$i) {
$files = array();
$dir = opendir('.');
while(($myfile = readdir($dir)) !== false)
{
if( is_file($myfile) )
{
$files[] = $myfile;
}
}
closedir($dir);
}
$end = microtime(true);
echo "Time for @markb's solution: readdir() = " . ($end-$start) . "\n";
$start = microtime(true);
for ($i = 0; $i < $n; ++$i) {
$files = glob('*', GLOB_NOSORT);
}
$end = microtime(true);
echo "Time for @Matchu's solution: glob(GLOB_NOSORT) = " . ($end-$start) . "\n";
$start = microtime(true);
for ($i = 0; $i < $n; ++$i) {
$files = array();
$dir = opendir('.');
while(($currentFile = readdir($dir)) !== false)
{
if ( $currentFile == '.' or $currentFile == '..' )
{
continue;
}
$files[] = $currentFile;
}
closedir($dir);
}
$end = microtime(true);
echo "Time for @lamas's solution: readdir() = " . ($end-$start) . "\n";