Cara menggunakan php scandir vs glob

dirLIST displays files and folders in a given HTTP/FTP directory. It has a wonderful interface with choice of Thumbnail or List view along with gorgeous icons for different file types. Includes a sleek gallery, web based mp3 player, file admin + more

Table of Contents

  • User Ratings
  • User Reviews
  • Additional Project Details
  • The opendir, readdir, and closedir Functions
  • The scandir Function
  • The glob Function
  • The dir Function
  • The FilesystemIterator Class
  • How do I get a list of files in a directory in PHP?
  • How do I view a directory in PHP?
  • How can I get a list of all the subfolders and files present in a directory using PHP?
  • What is directory Lister?

License

GNU General Public License version 2.0 (GPLv2)

Cara menggunakan php scandir vs glob

ConnectWise Control is a remote support solution for Managed Service Providers (MSP), Value Added Resellers (VAR), internal IT teams, and managed security providers. Fast, reliable, secure, and simple to use, ConnectWise Control helps businesses solve their customers' issues faster from any location. The platform features remote support, remote access, remote meeting, customization, and integrations with leading business tools.

User Ratings

4.9 out of 5 stars

★★★★★

★★★★

★★★

★★

ease 1 of 5 2 of 5 3 of 5 4 of 5 5 of 5 4 / 5

features 1 of 5 2 of 5 3 of 5 4 of 5 5 of 5 3 / 5

design 1 of 5 2 of 5 3 of 5 4 of 5 5 of 5 4 / 5

support 1 of 5 2 of 5 3 of 5 4 of 5 5 of 5 3 / 5

User Reviews

  • All
  • ★★★★★
  • ★★★★
  • ★★★
  • ★★
  • Very good script ! I'd like if there was the possibility to create new directory in Admin mode.

  • hmmm, Carefull if you are using this, just a search for dirlist exploit in google found a lot, including this http address packetstormsecurity.com/files/115381/dirlist-lfi.txt just try this on your own site by adding it to the url dirLIST_files/gallery_files/show_scaled_image.php?image_path=../index.php fixed it by changing //check if image is smaller than a required dimensions of 584 x 360...in which case just show the image if($image_attribs[0] <= $scaled_width && $image_attribs[1] <= $scaled_height) { readfile($image_path); exit; } to //check if image is smaller than a required dimensions of 584 x 360 and larger than 0 x 0...in which case just show the image if($image_attribs[0] <= $scaled_width && $image_attribs[1] <= $scaled_height && $image_attribs[0] > 0 && $image_attribs[1] >0) { readfile($image_path); exit; }

  • Dir-list is great! Thanks.

    1 user found this review helpful.

  • very good project, thanks!

    1 user found this review helpful.

Read more reviews >

Additional Project Details

Intended Audience

Advanced End Users, Developers

User Interface

Web-based

Programming Language

PHP
2007-04-26

In this article, we’ll discuss how you can get a list of all the files in a directory in PHP.

In your day-to-day PHP development, you often need to deal with a file system—for example, getting a list of files in a specific directory. PHP provides a few different ways of reading the contents of a directory easily. Today, we’ll go through all these methods, along with examples to understand how each one works.

The opendir, readdir, and closedir Functions

In this section, we’ll discuss the opendir, readdir, and closedir functions to see how you can use them to get a list of files in a specific directory.

The opendir function allows you to open up a directory handle, which you can use along with other functions for different operations on the directory. You need to pass the path to the directory in the first argument of the opendir function. If the directory path is valid, a directory handle resource will be returned.

The readdir function allows you to read a directory. You need to provide a valid directory handle in the first argument of the readdir function, and you can iterate over all the entries and get a list of all files in a directory.

The closedir function allows you to close the directory handle which is opened by the opendir function. It’s a good practice to use the closedir function once you’re done with your operations on the directory handle (which was initially opened by the opendir function).

Now, let’s see it in action in the following example.

<?php
$arrFiles = array();
$handle = opendir('/path/to/directory');

if ($handle) {
    while (($entry = readdir($handle)) !== FALSE) {
        $arrFiles[] = $entry;
    }
}

closedir($handle);
?>

Firstly, we’ve used the opendir function to get the directory handle. Next, we’ve used the readdir function to iterate over the $handle directory handle and store a list of files in the $arrFiles array.

The scandir Function

In this section, we’ll see how you can use the scandir function to get directory contents.

The scandir function is a pretty straightforward way to get a list of files and directories in a specific directory. You just need to pass a directory path which you want to read in the first argument of the scandir function.

Let’s go through the following example to understand how it works.

<?php
$arrFiles = scandir('/path/to/directory');
?>

As you can see, this provides a list of files and directories in a single call! If you just need to get a list of files in a directory, this is a better choice than using opendir, readdir, and closedir.

The glob Function

The glob function works similarly to the scandir function, with the difference that it allows you to specify a pattern for filtering and matching the files in the directory.

Let’s see how to use the glob function to read all the contents of a specific directory.

<?php
$arrFiles = glob('/path/to/dir/*');
?>

As you can see, we’ve passed the * pattern since we want to read all the contents. On the other hand, if you want to list only a specific type of file, you can do that as well, as shown in the following snippet.

<?php
$arrFiles = glob('/path/to/dir/*.txt');
?>

in this case, $arrFiles would contain only the names of .txt files.

The dir Function

The options that we’ve discussed so far allow you to read directory contents in a procedural way. In this section, we’ll see how you can use the dir function, which is an object-oriented mechanism for reading a directory.

When you use the dir function, and pass a directory path in the first argument, it returns an instance of the Directory class, which you can use subsequently to read the directory contents.

Let’s go through the following example to see how it works exactly.

<?php
$arrFiles = array();
$objDir = dir("/path/to/dir");

while (false !== ($entry = $objDir->read())) {
   $arrFiles[] = $entry;
}

$objDir->close();
?>

First, we used the dir function to initialize an instance of the Directory class into the $objDir variable. Next, we used the read method of the Directory class to iterate over all the entries.

The FilesystemIterator Class

In this section, we’ll see how you can use the FilesystemIterator class to read directory contents. The benefit of using the FilesystemIterator class is that you can extend the methods of this class, and it also provides a lot of other useful methods that allow you to fetch various information about the files.

Let’s go through the following example to understand how it works.

<?php
$arrFiles = array();
$iterator = new FilesystemIterator("/path/to/directory");

foreach($iterator as $entry) {
    $arrFiles[] = $entry->getFilename();
}
?>

As you can see, it’s really easy to use the FilesystemIterator class and iterate over the entries. In the above example, we’ve used the getFileName method of the FilesystemIterator class to get the file name. However, the FilesystemIterator class also provides several utility methods like getExtension, getSize, getPathname, and so on.

So that’s how you can use the FilesystemIterator class to get a list of files in a directory.

Conclusion

Today, we discussed different ways of listing all the files in a directory in PHP. Here's a table to compare each method to help you choose which one is right for you.

Function or ClassNotes
opendir, readdir and closedir functions not the simplest or most feature-rich method
scandir function simplest way to read a file list
glob function very simple and allows pattern matching
dir function object-oriented version of readdir
FilesystemIterator class can provide other info about files, like size or permissions

Did you find this post useful?

Software Engineer, FSPL, India

I'm a software engineer by profession, and I've done my engineering in computer science. It's been around 14 years I've been working in the field of website development and open-source technologies. Primarily, I work on PHP and MySQL-based projects and frameworks. Among them, I've worked on web frameworks like CodeIgnitor, Symfony, and Laravel. Apart from that, I've also had the chance to work on different CMS systems like Joomla, Drupal, and WordPress, and e-commerce systems like Magento, OpenCart, WooCommerce, and Drupal Commerce. I also like to attend community tech conferences, and as a part of that, I attended the 2016 Joomla World Conference held in Bangalore (India) and 2018 DrupalCon which was held in Mumbai (India). Apart from this, I like to travel, explore new places, and listen to music!

How do I get a list of files in a directory 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.

How do I view a directory in PHP?

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

How can I get a list of all the subfolders and files present in a directory using PHP?

PHP using scandir() to find folders in a directory To check if a folder or a file is in use, the function is_dir() or is_file() can be used. The scandir function is an inbuilt function that returns an array of files and directories of a specific directory.

What is directory Lister?

Directory Lister allows you to create, save, print, send via e-mail or store to database listings of files from selected directories on hard disks, CDs, DVDs, USB storages and network shares. Listing can be in HTML or text format.