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

查看:112
本文介绍了我可以用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控件停留几秒钟,然后是No Video。

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.

推荐答案

您的内容处理应该是:

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标头并设置内容 - 不会有什么坏处转移编码:

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天全站免登陆