这是创建歌曲对象的方法
public function getSong() {
return new Song($this->rackDir, $this->getLoadedDisc()->getName(), $this->song);
}
有宋课
class Song extends CD {
private $id;
public function __construct($rack, $name, $id)
{
$this->id = $id;
parent::__construct($rack, $name);
}
public function getSelectedSongId() {
return $this->id;
}
public function getSelectedSongPath() {
$list = $this->getSongsList();
return $list[$this->id];
}
}
public function getSongInfo () {
$data = [
'name' => $this->getName(),
'size' => $this->getSize(),
];
return $data;
}
public function getSize() {
$path = $this->getPath() . '/' . $this->getName();
return filesize($path);
}
public function getName() {
return $this->getSelectedSongPath();
}
}
还有CD类,我检查文件是否有音频扩展名。
class CD {
private $path;
private $name;
private $rack;
private $validExtensions;
public function __construct($rack, $name)
{
$this->rack = $rack . '/';
$this->name = $name;
$this->path = $this->rack . $this->name;
$this->validExtensions = ['mp3', 'mp4', 'wav'];
}
public function getPath() {
return $this->path;
}
public function getName() {
return $this->name;
}
public function getSongsList () {
$path = $this->rack . $this->name;
$songsList = [];
if (!is_dir($path)) {
return false;
}
if ($handle = opendir($path)) {
while (false !== ($file = readdir($handle)))
{
if ($file != "." && $file != ".." && in_array(strtolower(substr($file, strrpos($file, '.') + 1)), $this->validExtensions))
{
array_push($songsList, $file);
}
}
closedir($handle);
}
return $songsList;
}
}
我想检查File是否是真正的音频文件,而不仅仅是带有音频扩展名的文件? 是否有方法在PHP中执行此操作?
答案 0 :(得分:3)
Karlos是对的。 我发现了这个代码的解决方案。
public function validateFile (Song $song) {
$allowed = array(
'audio/mp4', 'audio/mp3', 'audio/mpeg3', 'audio/x-mpeg-3', 'audio/mpeg', 'audio/*'
);
$finfo = finfo_open(FILEINFO_MIME_TYPE);
$info = finfo_file($finfo, $song->getSelectedSongPath());
if (!in_array($info, $allowed)) {
die( 'file is empty / corrupted');
}
return $song;
}