I have an array I'm getting back from scandir, but it contains "."
and ".."
and I don't want it to.
My code:
$indir = scandir('../pages');
$fileextensions = array(".", "php", "html", "htm", "shtml");
$replaceextensions = str_replace($fileextensions, "", $indir);
I am doing a string replace on the file extensions, thus causing [0] and [1] to appear empty, but they are "."
and ".."
array(4) {
[0]=>
string(0) ""
[1]=>
string(0) ""
[2]=>
string(4) "test"
[3]=>
string(4) "home"
}
How would I remove the "."
and ".."
from the array?
You can use array_filter .
$indir = array_filter(scandir('../pages'), function($item) {
return !is_dir('../pages/' . $item);
});
Note this filters out all directories and leaves only files and symlinks. If you really want to only exclude only files (and directories) starting with .
, then you could do something like:
$indir = array_filter(scandir('../pages'), function($item) {
return $item[0] !== '.';
});
Fastest way to remove dots as files in scandir
$files = array_slice(scandir('/path/to/directory/'), 2);
From the PHP Manual
array_diff
will do what you're looking for:
$indir = scandir('../pages');
$fileextensions = array(".", "php", "html", "htm", "shtml");
$indir = array_diff($indir, array('.', '..'));
$replaceextensions = str_replace($fileextensions, "", $indir);
I am aware erknrio provided an answer for this, but here is a cleaner way of getting an array of files without directories (modified to be more efficient):
$dirPath = 'dashboard';
$dir = scandir($dirPath);
foreach($dir as $index => &$item)
{
if(is_dir($dirPath. '/' . $item))
{
unset($dir[$index]);
}
}
$dir = array_values($dir);
You can use this snippet. It returns just files in directory:
function only_files($dir_element) {
if (!is_dir($dir_element)) {
return $dir_element;
}
}
function givemefiles($dir) {
$scanned_dir = scandir($dir);
return array_filter($scanned_dir, "only_files");
}
$dir_path = '../pages';
givemefiles($dir_path);
echo "<pre>";
var_dump($scanned_dir);
echo "</pre>";
simply use preg_replace to remove all kind of hidden's file from directory
$files = array(".", "..", "html", ".~html", "shtml");
$newfiles = preg_grep('/^([^.])/', scandir($files));
If you need a clean function;
function getFiles($dir){
return array_values(array_filter(scandir($dir), function($file){
global $dir;
return !is_dir("{$dir}/{$file}");
}));
}
I think this function is both neat and will work well. Also, the keys of the returned array in this function are sorted correctly.
All other answers are good. I have a clean workaround that worked for me.
Use glob function that takes a regex and returns only files that match. It seems to exclude the .
and ..
directories, so using a simple
$indir = glob('../pages/*');
should work well.
function dscan( $path ){
global $dfiles;
if( !isset( $dfiles ) ){
$dfiles = array();
}
$files = preg_grep('/^.*\.php$/i', scandir($path));
foreach( $files as $file ){
if( !in_array( "$path/$file",$dfiles ) ){
$dfiles[] = array( 'path' => "$path/$file", 'name' => $file );
}
}
return $dfiles;
}
dscan( $path );
print_r( $dfiles );
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.