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. It lists the files and directories present inside the path specified by the user.

For example

$scan = scandir('myFolder');
foreach($scan as $file) {
   if (!is_dir("myFolder/$file")) {
      echo $file.'
';    } }


List of files and directories inside the path specified (if any)

The directory ‘myFolder’ is scanned using the ‘scandir’ function and the files and directories inside it are listed out. The ‘foreach’ loop is run over every file and if there is a file in the directory ‘myFolder’, it is echoed on the screen.

Updated on: 09-Apr-2020

10K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started