PHP - 检查音频文件是否未损坏

标签 php audio

这是创建歌曲对象的方法

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;
    }
}

我想检查文件是否是真实的音频文件,而不仅仅是带有音频扩展名的文件? 有没有在 PHP 中执行此操作的方法?

最佳答案

卡洛斯是对的。 我在下面的代码中找到了解决方案。

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;
}

关于PHP - 检查音频文件是否未损坏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44749510/

相关文章:

ios - 哪个 API 可以从 ios 和 osx 中的缓冲区播放音频?

android - 如何在android中播放旋律?

jquery - 单击 anchor 链接时淡入/淡出音乐

php - ffmpeg 编码在从脚本(php/c#/etc.)而不是从命令行执行时卡住

php - 移动wordpress站点后无法访问此站点

php - 通过 cron 作业创建文件夹并在网站端操作它的权限

python - Python中的Pyo:未定义名称 'Server'

ios - 如何在if语句中激活声音(objective-c)

PHP 在建立数据库连接时包含错误

PHP Carbon diffInMonths