Hướng dẫn php scandir only files

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?

Mark Amery

131k78 gold badges392 silver badges441 bronze badges

asked Feb 4, 2013 at 3:55

1

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] !== '.';
}];

answered Feb 4, 2013 at 4:00

leftclickbenleftclickben

4,48421 silver badges24 bronze badges

6

Fastest way to remove dots as files in scandir

$files = array_slice[scandir['/path/to/directory/'], 2]; 

From the PHP Manual

answered Feb 28, 2015 at 14:21

mintedskymintedsky

1,04313 silver badges18 bronze badges

5

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];

//php.net/manual/en/function.array-diff.php

answered Feb 4, 2013 at 4:00

0

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];

answered Mar 10, 2017 at 15:03

4

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 "
";
var_dump[$scanned_dir];
echo "
";

answered Nov 9, 2016 at 13:14

mrroot5mrroot5

1,6213 gold badges25 silver badges30 bronze badges

simply use preg_replace to remove all kind of hidden's file from directory

$files = array[".", "..", "html", ".~html", "shtml"];    
$newfiles = preg_grep['/^[[^.]]/', scandir[$files]];

answered Sep 22, 2017 at 6:01

Devdutt SharmaDevdutt Sharma

3811 gold badge2 silver badges10 bronze badges

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.

answered Oct 14, 2021 at 10:27

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.

answered Aug 23 at 12:24

EpanemuEpanemu

961 silver badge10 bronze badges

Not the answer you're looking for? Browse other questions tagged php scandir or ask your own question.

Chủ Đề