douyuan6490
2015-06-17 10:39
浏览 32
已采纳

如何检查目录中的音频文件是否与存储在数据库中的文件名匹配

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);
?>

图片转代码服务由CSDN问答提供 功能建议

我有一个包含音频文件路径的数据库。 此数据存储在名为Language1的列中。 实际的音频文件存储在名为Language1的文件夹中。

我想检查数据库中的路径是否与目录中的文件名匹配。

前10个文件位于文件夹中,但代码无法识别。 我希望代码读取数据库中的每一行,并将其与文件夹中的文件名进行比较。

 &lt;?php 
 $ servername =“localhost”;  
 $ user =“root”; 
 $ password =“root”; 
 $ dbname =“xxxx”; 
 
 //创建连接
 $ 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“文件$ Language1存在&lt; br /&gt;”; 
} 
else 
  {* 
 echo“文件$ Language1不存在&lt; br /&gt;”; 
} 
} 
mysqli_close($ conn); 
?&gt; 
   
  
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dongtuan1594 2015-06-17 10:57
    已采纳

    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";
        } 
        }
    
    点赞 打赏 评论

相关推荐 更多相似问题