PHP using scandir() to find folders in a directory

PHPServer Side ProgrammingProgramming

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.'\n';
   }
}

Output

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.

raja
Published on 09-Apr-2020 10:36:16
Advertisements