我可以使用 PHP 提供 MP3 文件吗? [英] Can I serve MP3 files with PHP?

查看:28
本文介绍了我可以使用 PHP 提供 MP3 文件吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

就像可以用 php 提供图像、用于 CAPTACHAS 等一样,是否可以对音频文件做同样的事情?

In the same way that it's possible to serve up images with php, for use in CAPTACHAS and such, is it possible to do the same with audio files?

我已经试过了

<?php

$track = "sometrack.mp3";

if(file_exists($track)) {
    header('Content-type: audio/mpeg');
    header('Content-length: ' . filesize($track));
    header('Content-Disposition: filename="sometrack.mp3"');
    header('X-Pad: avoid browser bug');
    header('Cache-Control: no-cache');
    print file_get_contents($track);
} else {
    echo "no file";
}

我使用的是 Safari,它可以播放 MP3 文件.它让 Safari 进入正确的模式,我获得了几秒钟的 Quicktime 控件,然后是无视频".

I'm using Safari, which can play MP3 files. It's kicking Safari into the right mode, I get the Quicktime controls for a few seconds, and then "No Video".

我正在努力保护文件免遭未经授权的下载,以防您想知道我为什么要这样做.

I'm trying to protect files from unauthorized download in case you're wondering why I'd want to do this.

推荐答案

你的 Content-Disposition 应该是:

Your Content-Disposition should be:

header('Content-Disposition: attachment; filename="sometrack.mp3"');

不知道这是否是问题所在.我还建议使用 readfile 输出文件:

Not sure if that's the problem though. I would also recommend using readfile to output the file:

readfile($rSong);

此外,使用详尽的 Content-Type 标头并设置 Content-Transfer-Encoding 也没什么坏处:

Also, it can't hurt to use an exhaustive Content-Type header, and set the Content-Transfer-Encoding:

header("Content-Transfer-Encoding: binary"); 
header("Content-Type: audio/mpeg, audio/x-mpeg, audio/x-mpeg-3, audio/mpeg3"); 

这篇关于我可以使用 PHP 提供 MP3 文件吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆