PHP Directory listing is not working, what am I doing wrong?. There are folders in the movies directory, but they are not showing in my output.
<?php
// declare the folder
$ourDir = "movies";
// prepare to read directory contents
$ourDirList = @opendir($ourDir);
// loop through the items
while ($ourItem = readdir($ourDirList))
{
// check if it is a directory
if (is_dir($ourItem))
{
echo $ourItem;
echo "<br />";
}
}
closedir($ourDirList);
?>
issue is when you are checking if $ourItem is a folder, you are forgetting is looking in the current directory for the folder.
see below.
// declare the folder
$ourDir = "movies";
// prepare to read directory contents
$ourDirList = @opendir($ourDir);
// loop through the items
while ($ourItem = readdir($ourDirList))
{
// check if it is a directory
if (is_dir($ourDir.DIRECTORY_SEPARATOR.$ourItem) )
{
echo $ourItem;
echo "<br />";
}
}closedir($ourDirList);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.