数据库仅存储一个文件的标记

时间:2018-03-20 18:11:59

标签: php arrays wamp mp3

我已经实现了PHP代码,该代码从目录中获取.mp3文件的数组,并从这些文件中提取元数据并将其显示在具有相应标题的表中。一切正常。

但是,当我将元数据附加到数据库时,它只会为其中一个.mp3文件存储标题,如标题,流派和年份,而不会存储多个文件。如何修复当前代码?

<?php


// gathering all mp3 files in 'mp3' folder into array
$sDir = 'mp3/';
$aFiles = array();
$rDir = opendir($sDir);
if ($rDir) {
  while ($sFile = readdir($rDir)) {
    if ($sFile == '.' or $sFile == '..' or !is_file($sDir . $sFile))
        continue;

    $aPathInfo = pathinfo($sFile);
    $sExt = strtolower($aPathInfo['extension']);
    if ($sExt == 'mp3') {
        $aFiles[] = $sDir . $sFile;
    }
}
closedir($rDir);
}

// new object of our ID3TagsReader class
$oReader = new ID3TagsReader();

// passing through located files ..
$sList = $sList2 = '';
foreach ($aFiles as $sSingleFile) {
  $aTags = $oReader->getTagsInfo($sSingleFile); 
  $Title = $aTags['Title'];
  $Author = $aTags['Author'];
  $AlbumAuthor = $aTags['AlbumAuthor'];
  $Year = $aTags['Year'];
  $Genre = $aTags['Genre'];

  $sList .= '<tr><td>'.$Title.'</td><td>'.$Author.'</td><td>'.$AlbumAuthor.'</td>
                <td>'.$Year.'</td><td>'.$Genre.'</td></tr>';
}


$dbhost = "localhost";
$dbname = "project";
$dbusername = "root";
$dbpassword = "";

$link = new PDO("mysql:host=$dbhost;dbname=$dbname", $dbusername, $dbpassword);

$statement = $link->prepare(
  "INSERT INTO mp3(Title, Author, AlbumAuthor, Year, Genre)
   VALUES(:Title, :Author, :AlbumAuthor, :Year, :Genre)");
$statement->execute(array(
  "Title" => "$Title",
  "Author" => "$Author",
  "AlbumAuthor" => "$AlbumAuthor",
  "Year" => "$Year",
  "Genre" => "$Genre"
));

0 个答案:

没有答案