Search code examples
phpfileforeachscandir

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


Here is the Code:

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


        }
    ?>

Task: I'm trying to only grab files that match the file that matches the presentation. All other files should not be considered, that way if it doesn't find a match it will show "File Not Available".


Solution

  • Just add one IF to know if array is empty or not :

    <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>
    

    Let me know if this is what you need.