问题
On a Windows system, a backup agent created temporary hidden files of nearly the same name as the original files, and in the same path. This was probably disturbing a process that used PHP scandir()
.
Now I was wondering if files on Windows having the hidden flag set are excluded by PHP scandir()
.
There are some articles about the hidden files in Linux style, how scandir()
should ignore files that start with a dot, but there is rarely no info about Windows files.
回答1:
I have tried this code on windows 7 and windows 8 & 8.1 and it surely excludes hidden files by flagging them out.
<?php
$location="path/to/a/folder/";
if( function_exists("scandir") ){
$file = scandir($location);
foreach($file as $this_file) {
/***Putting the condition here restricts hidden files and files starting with '.' and '~'...****/
if (is_hidden_file($location.$this_file) || $this_file == '.' || $this_file == '..' || $this_file[0]=='.' || $this_file[0]=='~' )
continue;
else {
var_dump($this_file);
}
}
}
function is_hidden_file($fn) {
$dir = "\"".$fn."\"";
$attr = trim(shell_exec("FOR %A IN (".$dir.") DO @ECHO %~aA"));
if($attr[3] === 'h')
return true;
return false;
}
?>
I see that you have mentioned in the question that there are ways to exclude files starting with '.' and stuff in linux but very less information about windows. Then check this out it not only eradicates files starting with '.' & '..' but also flags out the actually hidden files and will work for sure in windows.
回答2:
A short test shows that neither scandir()
nor glob()
or others take care of the hidden flag.
Here is the experiment and the result:
Parts:
- Windows 7
- PHP 5.6.9 (x86)
- Visual Studio 2012 Redistributable x86
So scandir()
does not hide files having the hidden flag set.
Next question is, can more powerful PHP commands like glob()
be configured.
Firstly, there is no parameter to deal with flags:
http://php.net/manual/de/function.glob.php
Secondly, there is this telling comment of Gabriel S. Luraschi:
http://php.net/manual/de/function.glob.php#110510
He recommends exec('dir ... \A ...')
. But on commercial hostings (if they run on Windows), this will not be allowed.
To be sure: use the Linux style and ignore files that start with a dot, like here:
Exclude hidden files from scandir
来源:https://stackoverflow.com/questions/30290663/does-php-scandir-exclude-hidden-files-under-windows