PHP has many functions related to the file system, which not only help you open files, but also display directory contents, move files, etc. Many people have even written web-based file managers using PHP.
First of all, I need to remind you something about the file path: in Windows you can use slash "/" or backslash "" in the file path, while other operating systems only use "/". For compatibility reasons, the following examples use the "/" notation:
The simple script below displays a basic directory listing. Comments are in the code and explain each step:
/* Give the full path to the directory you want to access in the variable $dir_name*/
$dir_name = "/home/me /";
/* Create a handle to open the result of the given directory*/
$dir = opendir($dir_name);
/* Start adding a piece of text to Where the list elements (file names) will be placed */
$file_list = "
/* Use the while statement to read all elements of the opened directory. If the file name is neither "." nor "..", then output the file name in the list */
while ($file_name = readdir($dir)) {
if (($ file_name != ".") && ($file_name != "..")) {
$file_list .= "
}
}
/* Terminate list*/
$file_list .= "
/* Close open directory handle and terminate PHP code Section*/
closedir($dir);
?>
< BODY>
Files in: echo "$dir_name"; ?>
echo "$file_list"; ?>