38

Why is it whenever I use scandir() I receive periods at the beginning of the array?

Array
(
    [0] => .
    [1] => ..
    [2] => bar.php
    [3] => foo.txt
    [4] => somedir
)
Array
(
    [0] => somedir
    [1] => foo.txt
    [2] => bar.php
    [3] => ..
    [4] => .
)
Mat
  • 202,337
  • 40
  • 393
  • 406
DividedDreams
  • 385
  • 1
  • 3
  • 5

6 Answers6

126

There are two entries present in every directory listing:

  • . refers to the current directory
  • .. refers to the parent directory (or the root, if the current directory is the root)

You can remove them from the results by filtering them out of the results of scandir:

$allFiles = scandir(__DIR__); // Or any other directory
$files = array_diff($allFiles, array('.', '..'));
phihag
  • 278,196
  • 72
  • 453
  • 469
  • 17
    Nice. This can also be condensed into one line: `$files = array_diff(scandir(__DIR__), array('.', '..'));` (see http://www.electrictoolbox.com/php-scandir-find-files/). – SharpC Aug 18 '15 at 14:43
  • 1
    I would like to note that, should you care about the indexing of $files, Dan Bray's answer is definitely worth looking at. I only wish I had scrolled down another half page earlier! – donutguy640 Jun 10 '19 at 14:11
31

Those are the current (.) and parent (..) directories. They are present in all directories, and are used to refer to the directory itself and its direct parent.

Mat
  • 202,337
  • 40
  • 393
  • 406
13

To remove . and .. from scandir use this function:

function scandir1($dir)
{
    return array_values(array_diff(scandir($dir), array('..', '.')));
}

The array_values command re-indexes the array so that it starts from 0. If you don't need the array re-indexing, then the accepted answer will work fine. Simply: array_diff(scandir($dir), array('..', '.')).

Dan Bray
  • 7,242
  • 3
  • 52
  • 70
8

In one line of code:

$files=array_slice(scandir('/path/to/directory/'), 2);
Unheilig
  • 16,196
  • 193
  • 68
  • 98
joash
  • 2,205
  • 2
  • 26
  • 31
  • 2
    This can cause problems if you have other files which are sorted before `.` and `..`. It's quite uncommon, as I think only `-` sorts before `.` and is valid in a filename, but it's worth remembering. – Steen Schütt Oct 22 '19 at 06:51
3

In Unix convention . is a link to the current directory while .. is a link to the parent directory. Both of them exist as a file in the directory index.

Jeremy
  • 4,797
  • 1
  • 18
  • 26
  • 2
    Can you quote a source that says this is a UNIX convention? DOS and Windows have `.` and `..` too, don't they? – phihag Aug 20 '11 at 14:38
  • 3
    Windows has those too, it's not specific to Unix. – Mat Aug 20 '11 at 14:38
  • 1
    It's a Unix convention; that doesn't prevent it from being a DOS and Windows convention too. – Keith Thompson Aug 20 '11 at 14:41
  • OK I would take that back. Both Windows and Unix use that convention. I only have the perception that most PHP server runs on Unix-based systems. – Jeremy Aug 20 '11 at 14:47
1

another one-line code solution to filter dots out

$files = array_filter(scandir($directory), function($file) {return strlen($file) > 2;});