Php 从文件夹中读取文件名仅保留扩展名为.iso的文件

Php 从文件夹中读取文件名仅保留扩展名为.iso的文件,php,Php,我有一个脚本,它从配置文件中获取一个字符串,并基于该字符串获取文件夹的文件名 我现在只需要iso文件。不确定最好的方法是检查.iso字符串还是有其他方法 <?php // Grab the contents of the "current.conf" file, removing any linebreaks. $dirPath = trim(file_get_contents('current.conf')).'/'; $fileList = scandir($

我有一个脚本,它从配置文件中获取一个字符串,并基于该字符串获取文件夹的文件名

我现在只需要iso文件。不确定最好的方法是检查.iso字符串还是有其他方法

<?php
    // Grab the contents of the "current.conf" file, removing any linebreaks.
    $dirPath = trim(file_get_contents('current.conf')).'/';

    $fileList = scandir($dirPath);

    if(is_array($fileList)) {
        foreach($fileList as $file) {
//could replace the below if statement to only proceed if the .iso string is present. But I am worried there could be issues with this.


         if ($file != "." and $file != ".." and $file != "index.php")
{

            echo "<br/><a href='". $dirPath.$file."'>" .$file."</a>\n";
    }
        } 
    }
    else echo $dirPath.' cound not be scanned.';
?>

试试这个:

    if(is_array($fileList)) {
        foreach($fileList as $file) {

            $fileSplode = explode('.',$file); //split by '.'
            //this means that u now have an array with the 1st element being the
            //filename and the 2nd being the extension

            echo (isset($fileSplode[1]) && $fileSplode[1]=='iso')?
                    "<br/><a href='". $dirPath.$file."'>" .$file."</a>\n":'');
        } 
    }
if(is_数组($fileList)){
foreach($fileList作为$file){
$fileSplode=explode('.',$file);//按'.'分割
//这意味着u现在有一个数组,第一个元素是
//文件名,第二个为扩展名
echo(isset($fileSplode[1])&&$fileSplode[1]=='iso')?
“
\n”:“); } }
试试这个:

    if(is_array($fileList)) {
        foreach($fileList as $file) {

            $fileSplode = explode('.',$file); //split by '.'
            //this means that u now have an array with the 1st element being the
            //filename and the 2nd being the extension

            echo (isset($fileSplode[1]) && $fileSplode[1]=='iso')?
                    "<br/><a href='". $dirPath.$file."'>" .$file."</a>\n":'');
        } 
    }
if(is_数组($fileList)){
foreach($fileList作为$file){
$fileSplode=explode('.',$file);//按'.'分割
//这意味着u现在有一个数组,第一个元素是
//文件名,第二个为扩展名
echo(isset($fileSplode[1])&&$fileSplode[1]=='iso')?
“
\n”:“); } }
如果您只需要扩展名为.iso的文件,为什么不使用:


如果您只需要扩展名为.iso的文件,那么为什么不使用:


而不是scandir()

如果您希望采用OOP风格,可以使用:

<?php

foreach (new DirectoryIterator($dirPath) as $fileInfo) {
   if($fileInfo->getExtension() == 'iso') {
      // do something with it
   }
}

?>

如果希望采用OOP风格,可以使用:

<?php

foreach (new DirectoryIterator($dirPath) as $fileInfo) {
   if($fileInfo->getExtension() == 'iso') {
      // do something with it
   }
}

?>


substr($file,-4,4)='.iso'有什么不对?对于固定的文件扩展名,不需要进行分解和数组操作。带有pathinfo_扩展名限定符的pathinfo()比使用explode()或substr()@Marc B更干净,而且我不认为这是
的原因。
案例这有关系吗?行动组在找文件
永远不会生成“.iso”作为子字符串,因此循环将继续进行卡车运输。此外,执行Mark baker的
glob()
调用效率更高。只执行
substr($file,-4,4)=='.iso'
有什么不对?对于固定的文件扩展名,不需要进行分解和数组操作。带有pathinfo_扩展名限定符的pathinfo()比使用explode()或substr()@Marc B更干净,而且我不认为这是
的原因。
案例这有关系吗?行动组在找文件
永远不会生成“.iso”作为子字符串,因此循环将继续进行卡车运输。此外,执行Mark baker的
glob()
调用效率更高;返回“iso”,您可以将其键入。可能重复$ext=pathinfo($filename,pathinfo_扩展名);返回'iso',您可以将其键入。由于某些原因,可能的重复我在数组中没有看到文件。我尝试只使用完整路径,而不使用变量$fileList=glob(“*”);回声计数($fileList);文件名是否以“2.1-2674”开头,或者“2.1-2674”是一个目录,在这种情况下,您应该使用$fileList=glob(“网站/2.1-2674/*”);由于某些原因,我没有看到数组中的文件。我尝试只使用完整路径,而不使用变量$fileList=glob(“*”);回声计数($fileList);文件名是否以“2.1-2674”开头,或者“2.1-2674”是一个目录,在这种情况下,您应该使用$fileList=glob(“网站/2.1-2674/*”);