怎么查看目录下的全部文件(包括子目录)?跪求大神帮忙。
现有代码如下:
<?php<br /> $handle=opendir("./");<br /> while(false!==($file=readdir($handle))){<br /> if ($file != "." && $file != ".."){<br /> echo "$file<br>";<br /> }<br /> }<br /> closedir($handle);<br /> <br /> ?>
上面代码只能列出本PHP文件所在目录下的文件夹和文件的名字,但不会显示本目录下子文件夹里的文件名。
现在有如下图的目录示意图(随便举个例,不是真的这结构):

如果这个PHP是放在文件夹A里的,请问怎么修改才能显示A目录下的所有文件(包括文件夹B、C、D、E、F、G、H、J、K)?最好分文件夹显示!
对了,最好加个判断,如果这个文件的后缀不是HTML的话,就用红色字体显示。
跪求大神帮忙。在此拜谢!
------解决方案--------------------
我自用的类里面的一个方法,你自己改一下吧
返回数组,二维的
参数说明:
$path 需要系统的绝对路径,结尾不带路径符“\”
$size =True 的时候把文件字节数也获取
$toCsv = 文件名(字符串),在返回数组结果的同时也写入到这个文件,格式为csv格式
** windows系统不能获得路径含有unicode字符的文件名,这不是程序问题,是php的问题
** 不列出空目录,只有文件,你可以参考手册给RecursiveDirectoryIterator加个参数就能得到空目录,但同时注意getsize可能会出错
<br /> public function listDir($path, $size=false, $toCsv=false)<br /> {<br /> if (false !== $toCsv) $newCsv = new SplFileObject($toCsv, 'wb');<br /> <br /> $items = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path));<br /> $arr = array();<br /> while (list($itemName, $item) = each($items))<br /> //foreach($dir as $itemName => $item)<br /> {<br /> $fileInfo = array();<br /> $fileInfo['fullpath'] = (string)$itemName;<br /> if ($size) $fileInfo['size'] = (string)$item->getSize();<br /> <br /> if (false !== $toCsv) $newCsv->fputcsv($fileInfo);<br /> $arr[] = $fileInfo;<br /> }<br /> return $arr;<br /> }<br />
我另外有一个可以获取windows unicode文件名的类方法,但相当麻烦,就不贴了
------解决方案--------------------
function show($dir)<br> { <br> echo "<b>{$dir}</b><br>";<br> $dir = realpath($dir) .'/*';<br> foreach(glob($dir, GLOB_MARK) as $f){<br> if( substr($f, -5) === '.html' ){<br> $f = "<span>{$f}</span>";<br> }<br> echo $f ."<br>".PHP_EOL;<br> if(is_dir($f)){<br> show($f); <div class="clear"> </div>

DependencyinjectioninPHPisadesignpatternthatenhancesflexibility,testability,andmaintainabilitybyprovidingexternaldependenciestoclasses.Itallowsforloosecoupling,easiertestingthroughmocking,andmodulardesign,butrequirescarefulstructuringtoavoidover-inje

PHP performance optimization can be achieved through the following steps: 1) use require_once or include_once on the top of the script to reduce the number of file loads; 2) use preprocessing statements and batch processing to reduce the number of database queries; 3) configure OPcache for opcode cache; 4) enable and configure PHP-FPM optimization process management; 5) use CDN to distribute static resources; 6) use Xdebug or Blackfire for code performance analysis; 7) select efficient data structures such as arrays; 8) write modular code for optimization execution.

OpcodecachingsignificantlyimprovesPHPperformancebycachingcompiledcode,reducingserverloadandresponsetimes.1)ItstorescompiledPHPcodeinmemory,bypassingparsingandcompiling.2)UseOPcachebysettingparametersinphp.ini,likememoryconsumptionandscriptlimits.3)Ad

Dependency injection provides object dependencies through external injection in PHP, improving the maintainability and flexibility of the code. Its implementation methods include: 1. Constructor injection, 2. Set value injection, 3. Interface injection. Using dependency injection can decouple, improve testability and flexibility, but attention should be paid to the possibility of increasing complexity and performance overhead.

Implementing dependency injection (DI) in PHP can be done by manual injection or using DI containers. 1) Manual injection passes dependencies through constructors, such as the UserService class injecting Logger. 2) Use DI containers to automatically manage dependencies, such as the Container class to manage Logger and UserService. Implementing DI can improve code flexibility and testability, but you need to pay attention to traps such as overinjection and service locator anti-mode.

Thedifferencebetweenunset()andsession_destroy()isthatunset()clearsspecificsessionvariableswhilekeepingthesessionactive,whereassession_destroy()terminatestheentiresession.1)Useunset()toremovespecificsessionvariableswithoutaffectingthesession'soveralls

Stickysessionsensureuserrequestsareroutedtothesameserverforsessiondataconsistency.1)SessionIdentificationassignsuserstoserversusingcookiesorURLmodifications.2)ConsistentRoutingdirectssubsequentrequeststothesameserver.3)LoadBalancingdistributesnewuser

PHPoffersvarioussessionsavehandlers:1)Files:Default,simplebutmaybottleneckonhigh-trafficsites.2)Memcached:High-performance,idealforspeed-criticalapplications.3)Redis:SimilartoMemcached,withaddedpersistence.4)Databases:Offerscontrol,usefulforintegrati


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Zend Studio 13.0.1
Powerful PHP integrated development environment

Notepad++7.3.1
Easy-to-use and free code editor

Dreamweaver Mac version
Visual web development tools

WebStorm Mac version
Useful JavaScript development tools

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.
