PHP Reading from Directories

Write a program in PHP to read from directory.

Solution

Let's implement a script to allow directory browsing of the uploaded content. Browsing directories is actually straightforward in PHP.

The following script can be used for this purpose -

<html>
<head>
<title>A divison table in PHP</title>
</head>
<body>
<?php
$current_dir = '/uploads/';
$dir = opendir($current_dir);

echo '<p>Upload directory is $current_dir</p/>';
echo '<p>Directory Listing:</p>';
echo '<ul>';
while($file = readdir($dir))
{
echo "<li>".$file."</li>"
}
echo '</ul>';
closedir($dir);
?>
</body>
</html>

The PHP function opendir() opens a directory handle. It takes directory path as parameter that to be opened. The readdir() function read from directory handle. The entries are returned in the order in which they are stored by the filesystem. The closedir() function is used to close a directory handle that is opened by the opendir() function.





Related Articles

PHP - Division table program
PHP - Prime Number Program
PHP - Print numbers 10 to 1 using recursion
PHP - Loop through an associative array
PHP - Differentiate between fgets, fgetss and fgetcsv
PHP - Set session on login
PHP - Convert text to speech
PHP - Copying or moving a file
PHP - Locking a file
PHP - CURL Cookie Jar
PHP MYSQL Advanced Search Feature PHP - Password Hashing
PHP - Emoji Unicode Characters
PHP - Age calculator
PHP - Get array key
PHP - Capitalize first letter
PHP - Set Timezone
PHP - Remove duplicates from array
PHP - Calculate percentage of total
PHP - Fibonacci Series Program
PHP - Insert image in database




Read more articles


General Knowledge



Learn Popular Language