为什么我没有得到文件和目录的名称?

时间:2013-10-22 11:02:40

标签: php windows

使用以下脚本,我尝试打印这些目录下的所有目录和文件的列表。但它没有这样做。

# 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

3 个答案:

答案 0 :(得分:1)

“/”代表基于unix的系统。你好像是Windows(“E:\”)。

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

答案 1 :(得分: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()方法,则应该传递目录的绝对路径。

答案 2 :(得分: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)