PHP Stream mp3文件 [英] PHP Stream mp3 file

查看:113
本文介绍了PHP Stream mp3文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要允许用户仅在不下载该文件的情况下流式传输它,我该怎么做? 另一个问题,此代码我有问题,文件先下载然后播放?

<?php $file = isset($_GET['q']) ? dirname(__FILE__) . base64_decode($_GET['q']) : false;
$file = urldecode(str_replace('/', '\\', $file));
$download = isset($_GET['d']) ? $_GET['d'] == 't' ? true : false : false;
if ($file) {
$disabelStr = 'DisableTrackDownload-';
$pattern = '/'.$disabelStr.'/';
$disableDownload = preg_match($pattern,$file);
$isFile = is_file($file);
// check if file exists
if($isFile){
    // Getting headers sent by the client.
    $headers = apache_request_headers();
    if(isset($headers['Connection'])){
        // stream audio files
        header("Content-Type: audio/mpeg");
        header('Content-Length:'.filesize($file));
        header('Content-Disposition: inline; filename="stream.file"');
        header('X-Pad: avoid browser bug');
        header("Cache-Control: no-store, no-cache, must-revalidate");
        header("Expires: -1");
        ob_clean();
        flush();
        readfile($file);
    }else{
        // disable download
        header ("HTTP/1.0 404 Not Found");
    }
    exit;
}
}
?>

解决方案

第1部分:一般的流式传输.

"MP3流"与HTTP下载不同.适当的流需要流媒体服务器(例如Shoutcast),并且通常是直播的转码版本或大型音频文件的点播版本,可以立即开始播放,而不必等待整个文件下载. >

即使客户端(例如Winamp,Quicktime Player等)能够在下载文件之前播放文件,通过HTTP提供的MP3文件也不构成真正的流".

为此,出于显而易见的原因,您不能从PHP提供MP3流(例如,PHP是一个短暂"过程,并不适合诸如HTTP上的流之类的长期请求). /p>

您描述的情况是提供MP3文件的标准HTTP下载,但使用的客户端可以立即开始播放.请注意,由于客户端的工作原理(通常),您无法阻止人们保存下载的文件.

我想您可以(通过User-agent标头)限制对已知客户端的访问,但这是邪恶的例子.请不要做这样的事情.


第2部分:您的代码

您的PHP代码使用Content-disposition标头向浏览器提示该文件应下载到文件系统,而不是传递给外部程序或插件的URL.删除标题后,您会发现某些浏览器将启动MP3播放器,但是其他浏览器仍会提示用户将文件保存到磁盘(通常是由于用户的偏好,或者可能是因为缺少已安装的MP3播放器).

此外,请勿使用HTTP 404拒绝请求.使用HTTP 403 Forbidden可以更准确地描述您的工作.

i need to allow the user to stream it only no download for that file how can i do that ? another question this code i have problem the file get downloaded first then play ?

<?php $file = isset($_GET['q']) ? dirname(__FILE__) . base64_decode($_GET['q']) : false;
$file = urldecode(str_replace('/', '\\', $file));
$download = isset($_GET['d']) ? $_GET['d'] == 't' ? true : false : false;
if ($file) {
$disabelStr = 'DisableTrackDownload-';
$pattern = '/'.$disabelStr.'/';
$disableDownload = preg_match($pattern,$file);
$isFile = is_file($file);
// check if file exists
if($isFile){
    // Getting headers sent by the client.
    $headers = apache_request_headers();
    if(isset($headers['Connection'])){
        // stream audio files
        header("Content-Type: audio/mpeg");
        header('Content-Length:'.filesize($file));
        header('Content-Disposition: inline; filename="stream.file"');
        header('X-Pad: avoid browser bug');
        header("Cache-Control: no-store, no-cache, must-revalidate");
        header("Expires: -1");
        ob_clean();
        flush();
        readfile($file);
    }else{
        // disable download
        header ("HTTP/1.0 404 Not Found");
    }
    exit;
}
}
?>

解决方案

Part 1: Streaming in general.

An "MP3 stream" is different from a HTTP download. A proper stream requires a streaming media server (e.g. Shoutcast) and is often a transcoded version of a live broadcast or an on-demand version of a large audio file where playback can begin immediately rather than waiting for the whole file to download.

Serving MP3 files via HTTP does not constitute bona-fide "streaming", even if the client (such as Winamp, Quicktime Player, etc) is capable of playing the file before it has downloaded.

To that end, you cannot serve MP3 streams from PHP for reasons that should be obvious (i.e. PHP is a 'short-lived' process and is not meant for long-lifed requests such as streams-over-HTTP).

The scenario you're describing is offering standard HTTP downloads of MP3 files but using clients that can begin playback immediately. Note that owing to how clients work (in general) you cannot stop people from saving a downloaded file.

I suppose you could restrict access to known clients (via the User-agent header) but that's an example of being evil. Please don't do anything like this.


Part 2: Your code

Your PHP code uses the Content-disposition header to hint to the browser that the file should be downloaded to the filesystem rather than the URL passed to an external program or plugin. Remove the header and you'll find that some browsers will launch an MP3 player, but others will still prompt the user to save the file to disk (usually because of user preference, or maybe the lack of an installed MP3 player).

Also, do not use HTTP 404 to deny a request. Use HTTP 403 Forbidden which more accurately describes what you're doing.

这篇关于PHP Stream mp3文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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