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

Output: File Not Available if it doesn't match the pattern

代码如下:

        $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';
                  }
                }


        }
    ?>

任务: 我试图只抓取与演示文稿相匹配的文件。不应考虑所有其他文件,这样如果找不到匹配项,它将显示 "File Not Available".

加一个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>

让我知道这是否是您需要的。

这应该适合你:

在这里,如果当前文件与模式匹配并且在过滤数组中或不在,我首先使用 glob() which already exclude . and ... Then I filter all files out which doesn't match the pattern with array_filter(). At the end I simply loop through all files and I check with 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";
    }

?>