In this short tutorial I will show how you can read directory into array that is on the server where the script is placed. We will get the names of all the files from that directory. For doing this you should create a handler for the directory by using opendir, passing the name of the directory.

After creating a handler to that directory we should iterate through all the files of that directory, so we gone use a loop until all the files from that directory are read:

 while ($file = readdir($handler)) {
    //get the name of the file
 }

After reading finishing the loop we should close the handler to that directory.

    closedir($handler);

Code:

   $directory="exampledir";
   // create a handler to the directory
    $dirhandler = opendir($directory);
 
    // read all the files from directory
    $nofiles=0;
    while ($file = readdir($dirhandler)) {
 
        // if $file isn't this directory or its parent 
        //add to the $files array
        if ($file != '.' && $file != '..')
        {
			$nofiles++;
			$files[$nofiles]=$file;                
        }   
    }
 
    //close the handler
    closedir($dirhandler);