PHP Miscellaneous
- Display source code of the website with PHP
- Get the contents of a directory in PHP
- Setting Cookies in PHP
- How to pass values between the pages in PHP?
- How to redirect to another page in PHP?
- Getting current date & time in PHP
- print (get) file extension in PHP
- Include a Class from another file in PHP
- Include php files when they\'re in different folders (directories)
- Import a CSS file in php without using HTML format
- Find similarity of two strings in PHP
- Generate random numbers in PHP
- Find remote IP address in PHP
- PHP program to validate an email address
- How to detect search engine bots with PHP?
- PHP program to calculate the md5 hash from plaintext
- PHP program to convert a string into base64
- PHP program to convert base64 into the simple string
- PHP program to send a text e-mail message
- PHP program to send an HTML email message
- PHP program to download the text file from the specified URL
- PHP program to demonstrate the use of $GLOBALS to access global variables
- PHP program to print the filename of the current executing PHP script
- PHP program to print the version of CGI used by the webserver
- PHP program to print the IP address of a hosted web server
- PHP program to print the server name or website name
- PHP program to print the version of the server software
- PHP program to print the server protocol
- PHP program to print the request method used to access the webpage
- PHP program to print the timestamp of the start of the request
- PHP program to demonstrate the use of $_REQUEST superglobal variable
- PHP program to demonstrate the use of $_POST superglobal variable
In this article, we will learn how to get contents of a directory in PHP. Following is the code?
Code - PHP
In this code, we create a function named list_files which expects the dir as argument, which is the directory to list. Inside this function, we first check if it is directory using is_dir() method. If true, then inside we open the directory using opendir() method and assign the result to handle variable, which is used to refer to this directory.
Next, we run a while loop until there are no more files using the readdir() which reads the contents of the directory. Inside this, we simply check if file is not equal to . (period) that is used for extensions, we can print the name of the file along with it's link using $dir.$file. After the loop is over, we close the directory using closedir() method.
need an explanation for this answer? contact us directly to get an explanation for this answer