Cara menggunakan php scandir without dots

The examples below look at a directory with the following, the same example directory as used in the read through directory and glob posts:

Show

bar.txt       A regular file
baz           A directory
foo.txt       A regular file
link2foo.txt  A symbolic link to foo.txt

Simple example

The following example reads all the files in the directory at /path/to/directory into an array using scandir() and then echos out the array:

$files = scandir("/path/to/directory");
print_r($files);

The output from the above using the example directory is as follows:

Array
(
    [0] => .
    [1] => ..
    [2] => 1.jpg
    [3] => 2.gif
    [4] => 3.png
    [5] => bar.txt
    [6] => baz
    [7] => foo.txt
    [8] => link2foo.txt
)

By default scandir() sorts the files into alphabetical order. To reverse the order pass a non-zero value as the second parameter like so:

In this article, we will see how to get all the files from the current or specified directory using the scandir() function in PHP. The scandir() function in PHP is an inbuilt function that is used to return an array of files and directories of the specified directory. The scandir() function lists the files and directories which are present inside a specified path. The directory, stream behavior, and sorting_order of the files and directories are passed as a parameter to the scandir() function and it returns an array of filenames on success, or false on failure. 

Syntax:

scandir(directory, sorting_order, context);

Parameters: The scandir() function in PHP accepts 3 parameters that are listed below:

  • directory: It is a mandatory parameter that specifies the path.
  • sorting_order: It is an optional parameter that specifies the sorting order. Alphabetically ascending order (0) is the default sort order. It can be set to SCANDIR_SORT_DESCENDING or 1 to sort in alphabetically descending order, or SCANDIR_SORT_NONE to return the result unsorted.
  • context: It is an optional parameter that specifies the behavior of the stream.

Return Value: It returns an array of filenames on success, or false on failure.

Errors And Exceptions:

  • The scandir() function throws an error of level E_WARNING if the directory specified is not a directory.
  • Doing a recursive scandir will on a directory that has many files will likely either slow down your application or cause a high rise in RAM consumption due to the large size of the generated array.

Approach: In order to get all the files from the particular directory, we need to specify the complete path of the file & store the path value in the variable as $mydir. Then use the scandir() function that will scan for the files in a current or specific directory & return an array of files and directories. By default, it will be aligned in the alphabetically ascending order & 0 as default sort order, 1 for sorting in alphabetically descending order & the SCANDIR_SORT_NONE  for unsorted order.

Example 1: The below example illustrate the scandir() function that will scan the files & return value will be in the ascending order.

PHP




<?php

   

  // Specifying directory

  $mydir

(
[0] => .
[1] => ..
[2] => aboutus.php
[3] => contact.php
[4] => index.php 
[5] => terms.php
)
0
(
[0] => .
[1] => ..
[2] => aboutus.php
[3] => contact.php
[4] => index.php 
[5] => terms.php
)
1
(
[0] => .
[1] => ..
[2] => aboutus.php
[3] => contact.php
[4] => index.php 
[5] => terms.php
)
2

 

  

(
[0] => .
[1] => ..
[2] => aboutus.php
[3] => contact.php
[4] => index.php 
[5] => terms.php
)
4

  

(
[0] => .
[1] => ..
[2] => aboutus.php
[3] => contact.php
[4] => index.php 
[5] => terms.php
)
6
(
[0] => .
[1] => ..
[2] => aboutus.php
[3] => contact.php
[4] => index.php 
[5] => terms.php
)
7$mydir
(
[0] => .
[1] => ..
[2] => aboutus.php
[3] => contact.php
[4] => index.php 
[5] => terms.php
)
9

 

  

 Array
(
[0] => terms.php
[1] => index.php 
[2] => contact.php
[3] => aboutus.php
[4] => ..
[5] => .
)
1

  

 Array
(
[0] => terms.php
[1] => index.php 
[2] => contact.php
[3] => aboutus.php
[4] => ..
[5] => .
)
3
(
[0] => .
[1] => ..
[2] => aboutus.php
[3] => contact.php
[4] => index.php 
[5] => terms.php
)
6
(
[0] => .
[1] => ..
[2] => aboutus.php
[3] => contact.php
[4] => index.php 
[5] => terms.php
)
9

 Array
(
[0] => terms.php
[1] => index.php 
[2] => contact.php
[3] => aboutus.php
[4] => ..
[5] => .
)
6

Output:

(
[0] => .
[1] => ..
[2] => aboutus.php
[3] => contact.php
[4] => index.php 
[5] => terms.php
)

Example 2: This example illustrates the scandir() function that will scan the files & return value will be in the descending order.

PHP




<?php

   

  // Specifying directory

  $mydir

(
[0] => .
[1] => ..
[2] => aboutus.php
[3] => contact.php
[4] => index.php 
[5] => terms.php
)
0
(
[0] => .
[1] => ..
[2] => aboutus.php
[3] => contact.php
[4] => index.php 
[5] => terms.php
)
1
(
[0] => .
[1] => ..
[2] => aboutus.php
[3] => contact.php
[4] => index.php 
[5] => terms.php
)
2

 

  

 Array
(
[0] => .
[1] => ..
[2] => contact.php
[3] => terms.php
[4] => index.php 
[5] => aboutus.php
)
7

  

(
[0] => .
[1] => ..
[2] => aboutus.php
[3] => contact.php
[4] => index.php 
[5] => terms.php
)
6
(
[0] => .
[1] => ..
[2] => aboutus.php
[3] => contact.php
[4] => index.php 
[5] => terms.php
)
7$mydir<?php2

 

  

 Array
(
[0] => terms.php
[1] => index.php 
[2] => contact.php
[3] => aboutus.php
[4] => ..
[5] => .
)
1

  

 Array
(
[0] => terms.php
[1] => index.php 
[2] => contact.php
[3] => aboutus.php
[4] => ..
[5] => .
)
3
(
[0] => .
[1] => ..
[2] => aboutus.php
[3] => contact.php
[4] => index.php 
[5] => terms.php
)
6
(
[0] => .
[1] => ..
[2] => aboutus.php
[3] => contact.php
[4] => index.php 
[5] => terms.php
)
9

 Array
(
[0] => terms.php
[1] => index.php 
[2] => contact.php
[3] => aboutus.php
[4] => ..
[5] => .
)
6

Output:

 Array
(
[0] => terms.php
[1] => index.php 
[2] => contact.php
[3] => aboutus.php
[4] => ..
[5] => .
)

Example 3: This example illustrates the scandir() function that will scan the files & return value will be in the unsorted order.