I am using the following code to get a list of images in a directory:
$files = scandir($imagepath);
but $files
also includes hidden files. How can I exclude them?
I am using the following code to get a list of images in a directory:
$files = scandir($imagepath);
but $files
also includes hidden files. How can I exclude them?
On Unix, you can use preg_grep
to filter out filenames that start with a dot:
$files = preg_grep('/^([^.])/', scandir($imagepath));
array_values
to the resulting array.
I tend to use DirectoryIterator for things like this which provides a simple method for ignoring dot files:
$path = '/your/path';
foreach (new DirectoryIterator($path) as $fileInfo) {
if($fileInfo->isDot()) continue;
$file = $path.$fileInfo->getFilename();
}
isDot()
doesn't ignore files that starts with .
. Just tried on my system PHP 5.3.5.
isDot
matches only if the file is .
or ..
. A quick test confirms that it will not match most dot files.
May 30, 2015 at 12:19
$files = array_diff(scandir($imagepath), array('..', '.'));
or
$files = array_slice(scandir($imagepath), 2);
might be faster than
$files = preg_grep('/^([^.])/', scandir($imagepath));
function nothidden($path) {
$files = scandir($path);
foreach($files as $file) {
if ($file[0] != '.') $nothidden[] = $file;
return $nothidden;
}
}
Simply use this function
$files = nothidden($imagepath);
preg_grep
because regexes frighten you, this is the way to go - check whether the filename starts with a dot by getting the first character and checking whether it's a dot. It's not rocket science, but it doesn't need to be. The "cleverer" incantations involving string functions used by some other answers here, besides being inefficient or wrong, are needlessly more complicated than this approach.
May 30, 2015 at 12:43
I encountered a comment from php.net, specifically for Windows systems: http://php.net/manual/en/function.filetype.php#87161
Quoting here for archive purposes:
I use the CLI version of PHP on Windows Vista. Here's how to determine if a file is marked "hidden" by NTFS:
function is_hidden_file($fn) { $attr = trim(exec('FOR %A IN ("'.$fn.'") DO @ECHO %~aA')); if($attr[3] === 'h') return true; return false; }
Changing
if($attr[3] === 'h')
toif($attr[4] === 's')
will check for system files.This should work on any Windows OS that provides DOS shell commands.
I reckon because you are trying to 'filter' out the hidden files, it makes more sense and looks best to do this...
$items = array_filter(scandir($directory), function ($item) {
return 0 !== strpos($item, '.');
});
I'd also not call the variable $files
as it implies that it only contains files, but you could in fact get directories as well...in some instances :)
use preg_grep to exclude files name with special characters for e.g.
$dir = "images/";
$files = preg_grep('/^([^.])/', scandir($dir));
Assuming the hidden files start with a .
you can do something like this when outputting:
foreach($files as $file) {
if(strpos($file, '.') !== (int) 0) {
echo $file;
}
}
Now you check for every item if there is no .
as the first character, and if not it echos you like you would do.
strpos
to check if the first character of a filename is a dot is unnecessarily complicated - and inefficient, since it searches whole string when you only care about one character. Also, it makes no sense at all to cast the literal 0
to an int
; it already is one. Just do if ($file[0] != '.') {...}
instead.
May 30, 2015 at 12:29
Use the following code if you like to reset the array index too and set the order:
$path = "the/path";
$files = array_values(
preg_grep(
'/^([^.])/',
scandir($path, SCANDIR_SORT_ASCENDING)
));
One line:
$path = "daten/kundenimporte/";
$files = array_values(preg_grep('/^([^.])/', scandir($path, SCANDIR_SORT_ASCENDING)));
scandir() is a built-in function, which by default select hidden file as well, if your directory has only . & .. hidden files then try selecting files
$files = array_diff(scandir("path/of/dir"),array(".","..")) //can add other hidden file if don't want to consider
I am still leaving the checkmark for seengee's solution and I would have posted a comment below for a slight correction to his solution.
His solution masks the directories(. and ..) but does not mask hidden files like .htaccess
A minor tweak solves the problem:
foreach(new DirectoryIterator($curDir) as $fileInfo) {
//Check for something like .htaccess in addition to . and ..
$fileName = $fileInfo->getFileName();
if(strlen(strstr($fileName, '.', true)) < 1) continue;
echo "<h3>" . $fileName . "</h3>";
}
$fileName
starts with a dot; I had to study the strstr
man page to even make sense of what you're doing here. It's also broken; this filters out not only filenames that start with a dot, but also file names with no dot in them at all (since in that case strstr
returns FALSE
and strlen(FALSE)
returns 0, which is less than 1. There's absolutely no need for this complexity; just do if ($fileName[0] == '.') continue
instead.
May 30, 2015 at 12:32