脚本宝典收集整理的这篇文章主要介绍了php – 查找具有任何扩展名的特定名称的文件,脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
<?PHP $compartment = "1"; /* HERE I NEED SOME SCRIPT TO FIND THE EXTENSION OF THE FILE NamE $compartment AND TO SAVE That AS A VARIABLE NAMED 'EXTENSION'.*/ if (file_exists($compartment.$extension)) { echo "$compartment.$extension exists! } else { echo "No file name exists that is called $compartment. Regardless of extension." } ?> <?PHP $compartment = "2"; /* HERE I NEED SOME SCRIPT TO FIND THE EXTENSION OF THE FILE NAME $compartment AND TO SAVE THAT AS A VARIABLE NAMED 'EXTENSION'.*/ if (file_exists($$compartment.$extension)) { echo "$compartment.$extension exists! } else { echo "No file name exists that is called $compartment. Regardless of extension." } ?>
谢谢!
glob()
.
$compartment = "2"; $files = glob("/path/to/files/$compartment.*"); // Will find 2.txt,2.PHP,2.gif // PRocess through each file in the list // and output ITs extension if (count($files) > 0) foreach ($files as $file) { $info = pathinfo($file); echo "File found: extension ".$info["extension"]."<br>"; } else echo "No file name exists called $compartment. Regardless of extension."
顺便说一句,你上面所做的就是为一个循环而哭泣.不要重复你的代码块,但将其中一个包装成:
$compartments = array(1,3,6,9); // or whichever compartments // you wish to run through foreach ($compartments as $compartment) { ..... insert code here ....... }
以上是脚本宝典为你收集整理的php – 查找具有任何扩展名的特定名称的文件全部内容,希望文章能够帮你解决php – 查找具有任何扩展名的特定名称的文件所遇到的问题。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。