设置Apache以在需要MP3文件时提供PHP服务 [英] Setting up apache to serve PHP when an MP3 file is requested

查看:116
本文介绍了设置Apache以在需要MP3文件时提供PHP服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究通过PHP提供MP3文件的方法,并且在形成了大量的帮助后,我得到了它的工作这里



然而,当我使用它时,该示例似乎不起作用作为像这样的音频标签中的来源

 < html> 
< head>
< title>音频标签实验< / title>
< / head>
< body>

这里是PHP

 <?php 

$ track =lilly.mp3;
$ b $ if(file_exists($ track))
{
header(Content-Transfer-Encoding:binary);
header(Content-Type:audio / mpeg,audio / x-mpeg,audio / x-mpeg-3,audio / mpeg3);
header('Content-length:'。filesize($ track));
header('Content-Disposition:filename =lilly.mp3');
标题('X-Pad:避免浏览器错误');
Header('Cache-Control:no-cache');

readfile($ track);
} else {
echono file;



$ b $ p
$ b所以我在想(这可能是一个非常糟糕的主意,你告诉我)我可能能够设置Apache来在有人请求.MP3时提供PHP文件。

b
$ b


  1. 这项工作是否可行

  2. 好主意/坏主意?
  3. 我需要去做?将AddType application / x-httpd-php .mp3放入httpd conf中吗?


解决方案

您的代码中存在一些错误:


  • 资源只能有一个 Content-Type 值。所以你必须决定你想使用的媒体类型。我建议 audio / mpeg

  • 你忘了在 Content-Disposition 。如果您只想提供文件名并且不想更改处置,请使用默认值内联



剩下的看起来很好。但如果找不到文件,我还会发送404状态码。

  $ track =lilly.mp3; 

if(file_exists($ track)){
header(Content-Type:audio / mpeg);
header('Content-Length:'。filesize($ track));
header('Content-Disposition:inline; filename =lilly.mp3');
标题('X-Pad:避免浏览器错误');
头('Cache-Control:no-cache');
readfile($ track);
退出;
} else {
header($ _ SERVER ['SERVER_PROTOCOL']。'404 Not Found',true,404);
回显无文件;
}


I'm working on a way to serve up MP3 files through PHP and after some help form the SO massive, I got it working here

However, that example doesn't appear to work when I use it as the source in an audio tag like this

<html>
    <head>
        <title>Audio Tag Experiment</title>
    </head>
    <body>

    <audio id='audio-element' src="music/mp3.php" autoplay controls>
    Your browser does not support the audio element.
    </audio>

    </body>
</html>

and here's the PHP

<?php

$track = "lilly.mp3";

if(file_exists($track))
{
header("Content-Transfer-Encoding: binary"); 
header("Content-Type: audio/mpeg, audio/x-mpeg, audio/x-mpeg-3, audio/mpeg3");
header('Content-length: ' . filesize($track));
header('Content-Disposition: filename="lilly.mp3"');
header('X-Pad: avoid browser bug');
Header('Cache-Control: no-cache');

readfile($track);
}else{
    echo "no file";
}

So I'm thinking (and this may be a really bad idea, you tell me) that I might be able to set up Apache to serve a PHP file when someone requests an .MP3.

So I've three questions

  1. Will this work
  2. Good Idea / Bad Idea?
  3. What would I need to do? Would putting "AddType application/x-httpd-php .mp3" int he httpd conf do it?

解决方案

There are some errors in your code:

  • A resource can only have one single Content-Type value. So you have to decide what media type you want to use. I suggest audio/mpeg.
  • You forgot to specify the disposition in Content-Disposition. If you just want give a filename and don’t want to change the disposition, use the default value inline.

The rest looks fine. But I would also send the 404 status code if the file cannot be found.

$track = "lilly.mp3";

if (file_exists($track)) {
    header("Content-Type: audio/mpeg");
    header('Content-Length: ' . filesize($track));
    header('Content-Disposition: inline; filename="lilly.mp3"');
    header('X-Pad: avoid browser bug');
    header('Cache-Control: no-cache');
    readfile($track);
    exit;
} else {
    header($_SERVER['SERVER_PROTOCOL'].' 404 Not Found', true, 404);
    echo "no file";
}

这篇关于设置Apache以在需要MP3文件时提供PHP服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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