Search code examples
phpmysqlmamp

How to check that the audio files in a directory match the file names stored in a database


I have a database that contains the pathway to audio files. This data is stored in a column called Language1. The actual audio file is stored in a folder called Language1.

I want to check that the pathways in the database match the filename that is in the directory.

The first 10 files are in the folder, but the code is not recognising that. I want the code to read through each row in the database and compare that with the file names in the folder.

<?php
$servername = "localhost";
$user = "root";
$password = "root";
$dbname = "xxxx";

// Create connection
$conn = mysqli_connect($servername, $user, $password, $dbname);
$query = "SELECT Language1 FROM Table";
$result = mysqli_query($conn, $query);

while($row = mysqli_fetch_array($result))
{

$path = "/file/to/path/English/$Language1"; 
$Language1 = $row['Language1'];

if (file_exists($path)) 
    { 
 echo "The file $Language1 exists<br/>";
        } 
else
    { *
    echo "The file $Language1 does not exist<br/>";
    } 
} 
mysqli_close($conn);
?>

Solution

  • First generate the relative file path of each file from your working directory to the folder where the audio files are uploaded. then check if that file exist using the file_exists function. You can update the status of each files to a column in the table.

    http://php.net/manual/en/function.file-exists.php.

    Edited - sample code

    $username = "Name";
    $result = mysqli_query($connection, $sql);
    while ($row = mysqli_fetch_row($result)) {
        $filename = $row['filename'];
            $path = "/folder/".$username."/english/". $filename; 
        if (file_exists($path)) { 
            echo "The file $filename exists"; 
        } else { 
            echo "The file $filename does not exist";
        } 
        }