如何从文件夹中显示不同的文件(mp3,jpeg)[关闭]

<?php
session_start();

include('config/connexionDB.php');
include('functions/search_user.php');

if (!isset($_SESSION['id'])){
    header('Location: index.php'); 
    exit;
}

// On récupère les informations de l'utilisateur connecté
$req = $DB->query("SELECT * 
    FROM posts
    ORDER BY id ");

$req = $req->fetchAll();

?>`
<?php
    foreach($req as $r){  

        $info = new SplFileInfo($r['fileNameNew']);

        $extaudio = array('.mp3', '.flac');

        $extimage = array('.png', '.jpg');

        if($info = $extaudio){
        ?>
             <div class="card" style="width: 45rem;">
                 <div class="card-body" align="center">
                     <h6 align="left"><?= $r['author'] ?></h6>
                     <?php echo"<audio src='uploads/".$r['fileNameNew']."' controls>;"; ?>
                     <h3><?= $r['content'] ?></h5>
                     <a href="#" class="btn btn-success">Like</a>
                     <a href="#" class="btn btn-danger">Dislike</a>
                 </div>
             </div><br>
        <?php
        }elseif($info = $extimage){
        ?>
             <div class="card" style="width: 45rem;">
                 <div class="card-body" align="center">
                     <h6 align="left"><?= $r['author'] ?></h6>
                     <h3><?= $r['content'] ?></h5>
                     <a href="#" class="btn btn-success">Like</a>
                     <a href="#" class="btn btn-danger">Dislike</a>
                     <?php echo "<img src='uploads/".$r['fileNameNew']."'>"; ?>
                 </div>
             </div><br>
         <?php
         }
    }
?>`

A few things.

$info = $extimage is wrong. Use == for a comparison. Both in the if and the elseif.

if($info == $extaudio){

A single equals assigns a value, ie. $age = 40 sets the age to 40, $age == 40 returns true or false if $age does equal 40 or not.

Secondly, $info = new SplFileInfo($r['fileNameNew']);. $info is an object, so comparing them directly will always retirn false.

http://php.net/manual/en/class.splfileinfo.php

Instead, get the file extension and check if it is in your array.

http://php.net/manual/en/splfileinfo.getextension.php

if(in_array($info->getExtension(), $extaudio)){

Hope this helps! Good luck