-1

使用以下脚本,我尝试打印这些目录下所有目录和文件的列表。但事实并非如此。

# Snippet that lists all the directories and files under that directory

$dir_list = scandir("/");
for($var=0;$var<count($dir_list);$var++) {
    if(is_dir($dir_list[$var])) {
        $dh = opendir($dir_list[$var]);
        echo "Name of directory : ".$dir_list[$var];
        echo "<br />";
        while(false !== ($filename = readdir($dh))) {
            echo $filename;
            echo "<br />";
        }
    }
    echo $dir_list[$var];echo "<br />";
}
#end

相反,这是我得到的输出。这是所有目录的列表。if当有这么多目录时,语句只执行一次。这是为什么 ?

        $AVG
$RECYCLE.BIN
.Trash-1000
01 - Raanjhanaa - DownloadMing.SE.mp3
05. TUNE YE PHOOL.mp3
16. THUMRI (TRADITIONAL).mp3
AVS Player
Aashiqui 2 {2013} Full Audio Songs Mp3 320kbps
Aye_Khuda_Ret_Ke_Sehar_-_(IndianWap.Mobi).mp3
Baazigar - Chhupana Bhi Nahi Aata (Pankaj Udhas)-(DJmaza.in).mp3
CV [Resume] templates
DWNL_VID
Delhi Belly 2011 Hindi 720p DVDRip x264 Ali Baloch Silver RG.mkv
Name of directory : Devdas - Blu-Ray - 720p - x264 - [DDR]
.
..
Devdas (Sample) - Blu-Ray - 720p - x264 - [DDR].mkv
Devdas (Screenshots) - Blu-Ray - 720p - x264 - [DDR]
Devdas - Blu-Ray - 720p - x264 - [DDR].gif
Devdas - Blu-Ray - 720p - x264 - [DDR].mkv
Devdas - Blu-Ray - 720p - x264 - [DDR].nfo
Devdas - Blu-Ray - 720p - x264 - [DDR]
Dil-Se.avi
Doc
DwnlData
Gangs of Wasseypur II 2012 Hindi 720p DvDRip CharmeLeon Silver RG.mkv
Gayatri-Mantra-Anuradha-Paudwal[www.savevid.com].mp4
Ghalib ghazal - 23.pdf
Ghar_Gulzar_Aapki Aankhon_mein.mp3
Gladiator[Extended.Edition]DvDrip.AC3[Eng]-aXXo
Google Drive
HD-Wallpapers
Hanuman-Chalisa-by-Gulshan-Kumar[www.savevid.com].3gp
I Am Kalam (2010) DVDRip(www.mastitorrents.com).avi
IMG_20120526_191139.jpg
Installed_Apps
Internet Download Manager 6.05 Build 7 Final
Jagjit Singh - Tera Bayaan Ghalib (2012) 128Kbps MP3.rar
Jai-Radha-Hare-Krishna[www.savevid.com].3gp
Java Persistence With Hibernate 2012
Khamoshi 1966
Lakshya.avi
MahaMantra-Hare-Rama-Hare-Krishna-by-Jagjit-Singh[www.savevid.com].3gp
New Folder
New folder (2)
OS images
Om-Gan-Ganapataye-Namo-Namah[www.savevid.com].3gp
Om-Namah-Shivay-(Beautiful-Bhajan-By-Jagjit-Singh)[www.savevid.com].3gp
Pending
PseudoCompiler.cnt
PseudoCompiler.exe
PseudoCompiler.hlp
Ra-One
Ravi Shankar
Rhythm and Blues
ST5UNST.LOG
Shiv Kumar Sharma Torrent
Shiv-Mahamrityunjaya-Mantra[www.savevid.com].3gp
Shree-Hanuman-Chalisa-Anup-Jalota[www.savevid.com].mp4
Special 26 - 2013 DVDRip XviD AC3 M-SuB xRG
System Volume Information
THE-GODFATHER.Part-3.DVDrip[vice]
Taj-Mahal--Secrets-n-Mysteries[www.savevid.com].flv
Tera Bayaan Ghalib Part 1.wmv
Tera Bayaan Ghalib Part 2.wmv
Tera byaan ghalib
The Godfather - II
The Godfather part 1.avi
Untitled Folder
VDI
Vicky Donor - DVDRip - XviD - 1CDRip - [DDR]
VideoLAN - VLC media player for Fedora.htm
VideoLAN - VLC media player for Fedora_files
Visual Fox Pro 9
Visual Fox Pro 9.zip
Windows_Movie_Maker
adding security_niit_glassfish
animated gifs
flipkart
found.000
games_setup
ghazals
htmlProject
httpd-2.2.17
java project
logs
mehdi hassan...arz e niyaaz e isqh ke qabil nahi raha.mp3
music
music_videos
niit
non_music_vidoes
old paper
only jagjit
pagefile.sys
pics
program works
softwares
suhail_1.2
sunny01(www.songs.pk).mp3
tie_1.JPG
tie_2.jpg
4

3 回答 3

1

应该是这样的,

$dir_list = scandir("/");

for($var=0;$var<count($dir_list);$var++) {
    if(is_dir($dir_list[$var]))
    {
        $dh = opendir($dir_list[$var]);
        echo "<b>Name of directory : ".$dir_list[$var].'</b><br>';
        if($dh!=false && $dh!= '.' && $dh!='..')
        {
          while(false != ($filename = readdir($dh))) {
              echo $filename;
          }
        }
    }
    else
    {
        echo "Name of file : ".$dir_list[$var].'<br>';
    }
}

如果您使用的是 windows 而不是scandir()方法,您应该传递目录的绝对路径。

于 2013-10-22T11:13:51.703 回答
1

“/”代表基于 Unix 的系统。你的似乎是 Windows ("E:\")。

尝试更改scandir("/")scandir('E:\')

于 2013-10-22T11:06:21.127 回答
0

由于您的问题完全不同,我做了另一个答案:

1°你需要递归2°而不是使用复杂的函数(opendir,readdir)scandir就足够了

根据您的代码:

<?php

function displayRecursive($path){ 
    $dir_sep = DIRECTORY_SEPARATOR;
    $path = rtrim($path, '/').'/'; //
$dir_list = scandir($path);
    echo '<ul>';
foreach($dir_list as $file){
        if ($file[0] == '.')
            continue;

        echo '<li>';
        if(is_dir($path.$dir_sep.$file)){
            echo "Name of directory : ".$dir_list[$var];
            displayRecursive($path.$dir_sep.$file);
        }
        else{
            echo $filename;
        }
        echo '</li>';
}
    echo '</ul>';

}
$first_dir = '/';
displayRecursive($first_dir)
于 2013-10-22T12:41:39.097 回答