输出:如果与模式不匹配,则文件不可用

时间:2015-04-09 22:19:01

标签: php file foreach scandir

这是守则:
            

        $course = htmlspecialchars($_GET["course"]);
        $db = odbc_connect('#');

        // Course Heading
        $courseheading = "SELECT * 
        FROM tbsessions, tbcourses, tbpresentations 
        WHERE (tbsessions.courseId = tbcourses.courseId) 
        AND (tbpresentations.courseSessionId = tbsessions.courseSessionId) 
        AND (tbsessions.courseSessionID = '$course') 
        AND (tbsessions.category NOT IN ('M','S')) 
        ORDER BY startDate DESC, courseTitle";

        $courseRS = odbc_exec($db, $courseheading);
        $courseTitle = odbc_result($courseRS, "courseTitle");
        $subtitle = odbc_result($courseRS, "subtitle");

            echo '<div class="title">
                <h1>'.$courseTitle.'</h1>
            <p>'.$subtitle.'</p></div>';

        // Presentation Information
        $query = "SELECT *
        FROM tbspeakers s join tbpresentations p on
        s.spkrId = p.spkrId

        join tbsessions ss on
        p.courseSessionid = ss.courseSessionId

        join tbcourses c on
        ss.courseId = c.courseId

        WHERE (ss.courseSessionID = '$course')

        ORDER BY pTitle";


        $result = odbc_exec($db, $query);
    // Generate presentation and speaker information    
        while(odbc_fetch_row($result)){
                $courseTitle = odbc_result($result, "courseTitle");
                $subtitle = odbc_result($result, "subtitle");
                $pTitle = odbc_result($result, "pTitle");
                $fname = odbc_result($result, "fname");
                $lname = odbc_result($result, "lname");
                $degree = odbc_result($result, "degree");
                $pSDateTime = odbc_result($result, "pSDateTime");


                echo '<p><strong>'.$pTitle. '</strong> - ' . $fname . ' ' . $lname . ', ' . $degree . '</p>';
                $courseTitle = explode(" ", $courseTitle);
                $course = "";
                $courseTitle = preg_replace('/\(|\)/', '', $courseTitle);
                foreach ($courseTitle as $value) {
                    $course .= substr($value, 0, 2);
                }
                $pSDateTime = str_replace('-', '', $pSDateTime);
                $pSDateTime = str_replace(':', '', $pSDateTime);
                $pSDateTime = str_replace(' ', '_', $pSDateTime);
                $pSDateTime = substr($pSDateTime, 0, -2);
                $presentation = strToLower($course). '_' .$pSDateTime;

            // Generate presentation download link
                $dir    = '../assets/training/archive';
                $files = scandir($dir);
                $imgarray = array();
                foreach($files as $file) {
                  if(fnmatch($presentation.'.*',$file)) {
                    $imgarray[] = $file;
                  }
                }

                foreach($imgarray as $download) {
                  if(isset($download)) {
                      echo '<p><a href="/assets/training/archive/'.$download.'">Download</a></p> <br><br>';
                  }
                  else {
                      echo 'File Not Available';
                  }
                }


        }
    ?>

任务: 我试图只抓取与匹配演示文稿的文件匹配的文件。不应考虑所有其他文件,如果找不到匹配项,则会显示“文件不可用”。

2 个答案:

答案 0 :(得分:0)

这应该适合你:

在这里,我首先从您的目录中获取glob()已排除...的所有文件。然后我使用array_filter()过滤掉与模式不匹配的所有文件。最后,我只是循环遍历所有文件,如果当前文件与模式匹配并且在过滤后的数组中,则检查in_array()

<?php

    $dir = "../assets/training/archive";
    $files = glob("$dir/*.*");

    $filteredFiles = array_filter($files, function($v)use($presentation){
        return fnmatch($presentation.'.*', $v);
    });

    foreach($files as $file) {
        if(in_array($file, $filteredFiles)) 
            echo "<p><a href='/assets/training/archive/'". $file . "'>Download</a></p><br><br>";
        else
            echo "File Not Available";
    }

?>

答案 1 :(得分:-1)

只需添加一个IF即可知道数组是否为空:

<html>
  <body>
<?php
$dir = "../assets/training/archive";
$files = scandir($dir);
$imgarray = array();
$presentation = '*.php'; // ENTER ANY FILENAME OR PATTERN HERE.
foreach($files as $file)
  if(fnmatch($presentation,$file))
     $imgarray[] = $file;

if ( count( $imgarray ) == 0 ) // NO MATCHES.
     echo 'No files available';
else foreach($imgarray as $download)
       echo '<p><a href="/assets/training/archive/'.$download.'">Download</a></p> <br><br>';
?>
  </body>
</html>

如果您需要,请告诉我。