我正在尝试递归扫描文件夹结构并使用指向每个找到的文件的路径填充数组。
但是print_r($fileArray)
给我一个空数组作为输出?像这样:Array ( )
错误在哪里?
function getFilePaths($path){
$structureArray = [];
$fileArray = [];
foreach(new DirectoryIterator($path) as $item){
if($item->isDir()){
if(!$item->isDot()) {
$structureArray[$item->getFilename()] = getFilePaths($item->getPathname());
}
continue;
}
$fileArray[] = '"'. $item->getPathname() .'"';
}
return $fileArray;
}
更新
var_dump($ item)的输出;在你的foreach中:
object(DirectoryIterator)#9 (4) { ["pathName":"SplFileInfo":private]=> string(68) "music\sorted\3 Doors Down\Seventeen Days\01 Right Where I Belong.mp3" ["fileName":"SplFileInfo":private]=> string(27) "01 Right Where I Belong.mp3" ["glob":"DirectoryIterator":private]=> bool(false) ["subPathName":"RecursiveDirectoryIterator":private]=> string(0) "" } object(DirectoryIterator)#9 (4) { ["pathName":"SplFileInfo":private]=> string(59) "music\sorted\3 Doors Down\Seventeen Days\02 It's Not Me.mp3" ["fileName":"SplFileInfo":private]=> string(18) "02 It's Not Me.mp3" ["glob":"DirectoryIterator":private]=> bool(false) ["subPathName":"RecursiveDirectoryIterator":private]=> string(0) "" } object(DirectoryIterator)#9 (4) { ["pathName":"SplFileInfo":private]=> string(58) "music\sorted\3 Doors Down\Seventeen Days\AlbumArtSmall.jpg" ["fileName":"SplFileInfo":private]=> string(17) "AlbumArtSmall.jpg" ["glob":"DirectoryIterator":private]=> bool(false) ["subPathName":"RecursiveDirectoryIterator":private]=> string(0) "" } object(DirectoryIterator)#9 (4) { ["pathName":"SplFileInfo":private]=> string(51) "music\sorted\3 Doors Down\Seventeen Days\Folder.jpg" ["fileName":"SplFileInfo":private]=> string(10) "Folder.jpg" ["glob":"DirectoryIterator":private]=> bool(false) ["subPathName":"RecursiveDirectoryIterator":private]=> string(0) "" } object(DirectoryIterator)#9 (4) { ["pathName":"SplFileInfo":private]=> string(59) "music\sorted\3 Doors Down\The Better Life\01 Kryptonite.mp3" ["fileName":"SplFileInfo":private]=> string(17) "01 Kryptonite.mp3" ["glob":"DirectoryIterator":private]=> bool(false) ["subPathName":"RecursiveDirectoryIterator":private]=> string(0) "" } object(DirectoryIterator)#9 (4) { ["pathName":"SplFileInfo":private]=> string(54) "music\sorted\3 Doors Down\The Better Life\02 Loser.mp3" ["fileName":"SplFileInfo":private]=> string(12) "02 Loser.mp3" ["glob":"DirectoryIterator":private]=> bool(false) ["subPathName":"RecursiveDirectoryIterator":private]=> string(0) "" } object(DirectoryIterator)#9 (4) { ["pathName":"SplFileInfo":private]=> string(59) "music\sorted\3 Doors Down\The Better Life\AlbumArtSmall.jpg" ["fileName":"SplFileInfo":private]=> string(17) "AlbumArtSmall.jpg" ["glob":"DirectoryIterator":private]=> bool(false) ["subPathName":"RecursiveDirectoryIterator":private]=> string(0) "" } object(DirectoryIterator)#9 (4) { ["pathName":"SplFileInfo":private]=> string(52) "music\sorted\3 Doors Down\The Better Life\Folder.jpg" ["fileName":"SplFileInfo":private]=> string(10) "Folder.jpg" ["glob":"DirectoryIterator":private]=> bool(false) ["subPathName":"RecursiveDirectoryIterator":private]=> string(0) "" } object(DirectoryIterator)#5 (4) { ["pathName":"SplFileInfo":private]=> string(55) "music\unsorted\01 Club Rocker (Mike Candys Radio Ed.mp3" ["fileName":"SplFileInfo":private]=> string(40) "01 Club Rocker (Mike Candys Radio Ed.mp3" ["glob":"DirectoryIterator":private]=> bool(false) ["subPathName":"RecursiveDirectoryIterator":private]=> string(0) "" } object(DirectoryIterator)#7 (4) { ["pathName":"SplFileInfo":private]=> string(69) "music\unsorted\tagged\257ers - Bis dahin bin ich tot ft. Favorite.mp3" ["fileName":"SplFileInfo":private]=> string(47) "257ers - Bis dahin bin ich tot ft. Favorite.mp3" ["glob":"DirectoryIterator":private]=> bool(false) ["subPathName":"RecursiveDirectoryIterator":private]=> string(0) "" } object(DirectoryIterator)#7 (4) { ["pathName":"SplFileInfo":private]=> string(62) "music\unsorted\tagged\257ers - Seite an Seite ft. Favorite.mp3" ["fileName":"SplFileInfo":private]=> string(40) "257ers - Seite an Seite ft. Favorite.mp3" ["glob":"DirectoryIterator":private]=> bool(false) ["subPathName":"RecursiveDirectoryIterator":private]=> string(0) "" }
答案 0 :(得分:5)
我使用此代码生成目录中的php文件列表,并删除扩展名" .php"来自每个文件名
不使用DirectoryIterator
function getFiles($directory){
$files = array_diff(scandir($directory), array('.','..','.htaccess'));
foreach($files as $f){
if(strstr($f, ".php"))
$fileArray[] = str_replace(".php","",$f);
else {
$fileArray[$f] = getFiles($directory."/".$f);
}
}
return $fileArray;
}
P.S。所有文件名都在一个数组中,甚至是子目录中的文件
答案 1 :(得分:0)
我不知道你是否已经尝试过,但df1 = df1[(df1['ShiftScheduledEnd']) < df2.CurrentDate]
应该返回给定文件夹中的所有文件。它内置于php并且相当短。