Cara menggunakan php scandir remove dots

Welcome to a quick tutorial on how to list files and folders in PHP. Want to get the contents of a folder in PHP?

Table of Contents

  • QUICK SLIDES
  • TABLE OF CONTENTS
  • DOWNLOAD & NOTES
  • QUICK NOTES
  • EXAMPLE CODE DOWNLOAD
  • METHOD 1) SCANDIR
  • METHOD 2) GLOB
  • METHOD 3) OPENDIR
  • EXTRA) RECURSIVE GET SUB-FOLDERS
  • LINKS & REFERENCES
  • TUTORIAL VIDEO
  • INFOGRAPHIC CHEAT SHEET
  • How do I view a directory in PHP?
  • How can I get directory file in PHP?
  • What is __ DIR __ in PHP?
  • How show all files in a directory in PHP?

The common ways to list files and folders in PHP are:

  1. Use scandir() to get an array of files and folders – $contents = scandir("FOLDER");
  2. Use glob() to selectively get files and folders.
    • $pictures = glob("FOLDER/*.{jpg,jpeg,png,gif}", GLOB_BRACE);
    • $folders = glob("FOLDER/*", GLOB_ONLYDIR);
  3. Lastly, use opendir() to loop through a folder.
    • $fh = opendir("FOLDER");
    • while (($entry = readdir($fh)) !== false) { echo $entry; }
    • fclose($fh);

That should cover the basics, but read on for more examples!

ⓘ I have included a zip file with all the source code at the start of this tutorial, so you don’t have to copy-paste everything… Or if you just want to dive straight in.

QUICK SLIDES

Cara menggunakan php scandir remove dots

TABLE OF CONTENTS

DOWNLOAD & NOTES

Firstly, here is the download link to the example code as promised.

QUICK NOTES

If you spot a bug, feel free to comment below. I try to answer short questions too, but it is one person versus the entire world… If you need answers urgently, please check out my list of websites to get help with programming.

EXAMPLE CODE DOWNLOAD

Click here to download all the example source code, I have released it under the MIT license, so feel free to build on top of it or use it in your own project.

All right, let us now get into the various examples of how to list files and folders in PHP.

METHOD 1) SCANDIR

1-scandir.php

<?php
// (A) GET ALL FILES + FOLDERS
$contents = scandir(__DIR__);

// PASS IN A SECOND PARAMETER TO SORT
//$contents = scandir($target, SCANDIR_SORT_DESCENDING);
//$contents = scandir($target, SCANDIR_SORT_NONE);

// (B) LOOP THROUGH ALL FILES + FOLDERS
foreach ($contents as $c) {
  if ($c!="." && $c!="..") {
    echo is_dir($c) ? "DIR" : "FILE" ;
    echo " - $c<br>";
  }
}

This is as easy as it gets, just use scandir("FOLDER") to get an array of all the files and folders. But take note, we can’t really “customize” this function to get “certain file types only”, and it is potentially bad for folders with a lot of files.

METHOD 2) GLOB

2-glob.php

<?php
// (A) GET ALL PHP + TEXT FILES
$scripts = glob(__DIR__ . DIRECTORY_SEPARATOR . "*.{php,txt}", GLOB_BRACE);
print_r($scripts);

// (B) GET FOLDERS ONLY
$folders = glob(__DIR__ . DIRECTORY_SEPARATOR . "*" , GLOB_ONLYDIR);
print_r($folders);

To get a “customized files list”, use the glob() function instead – We can pretty much restrict to any file type, get folders only, and even do very specific searches.

METHOD 3) OPENDIR

3-opendir.php

<?php
// (A) OPEN FOLDER + READ ENTRIES
$target = __DIR__;
if ($fh = opendir($target)) {
  while (($entry = readdir($fh)) !== false) {
    // (B) IGNORE "." AND ".."
    if ($entry!="." && $entry!="..") {
      // (C) FILE OR FOLDER?
      $thisE = $target . DIRECTORY_SEPARATOR . $entry;
      echo is_dir($thisE) ? "Directory" : "File";
      echo " - $thisE<br>";
    }
  }
  closedir($fh);
}

Have a massive folder with thousands of files? This is how we should deal with it – Read the entries one-by-one.

EXTRA) RECURSIVE GET SUB-FOLDERS

4-recursive.php

<?php
// (A) RECURSIVE GET FILES + FOLDERS
function getFF ($path) {
  // (A1) GET FILES
  $files = glob("$path*");
 
  // (A2) GET FOLDERS
  $folders = glob("$path*", GLOB_ONLYDIR);
 
  // (A3) OUTPUT CURRENT PATH
  echo "<h2>$path</h2><br>";
  print_r($files);
  print_r($folders);

  // (A4) RECURSIVE LOOP
  if (count($folders)!=0) {
    foreach ($folders as $f) {
      getFF($f . DIRECTORY_SEPARATOR);
    }
  }
}
 
// (B) GO!
getFF(__DIR__ . DIRECTORY_SEPARATOR);

Lastly, getting the sub-folders are kind of tricky – We need a recursive function that will automatically drill into the sub-folders.

  • Scandir – PHP
  • Readdir – PHP
  • Glob – PHP

TUTORIAL VIDEO

INFOGRAPHIC CHEAT SHEET

How To List Files Folders In PHP (Click To Enlarge)

THE END

Thank you for reading, and we have come to the end. I hope that it has helped you to better understand, and if you want to share anything with this guide, please feel free to comment below. Good luck and happy coding!

How do I view a directory in PHP?

PHP scandir() Function $b = scandir($dir,1);

How can I get directory file in PHP?

you can esay and simply get list of file in folder in php. The scandir() function in PHP is an inbuilt function which 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.

What is __ DIR __ in PHP?

The __DIR__ can be used to obtain the current code working directory. It has been introduced in PHP beginning from version 5.3. It is similar to using dirname(__FILE__). Usually, it is used to include other files that is present in an included file.

How show all files in a directory in PHP?

The common ways to list files and folders in PHP are:.

Use scandir() to get an array of files and folders – $contents = scandir("FOLDER");.

Use glob() to selectively get files and folders. $pictures = glob("FOLDER/*. {jpg,jpeg,png,gif}", GLOB_BRACE); ... .

Lastly, use opendir() to loop through a folder. $fh = opendir("FOLDER");.