设置 apache 在请求 MP3 文件时为 PHP 提供服务

Setting up apache to serve PHP when an MP3 file is requested(设置 apache 在请求 MP3 文件时为 PHP 提供服务)
本文介绍了设置 apache 在请求 MP3 文件时为 PHP 提供服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我正在研究一种通过 PHP 提供 MP3 文件的方法,经过一些帮助形成如此庞大的文件,我让它工作了 这里

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>

这里是 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";
}

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

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.

所以我有三个问题

  1. 这行得通吗
  2. 好主意/坏主意?
  3. 我需要做什么?将AddType application/x-httpd-php .mp3"放入 httpd conf 中可以吗?

推荐答案

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

There are some errors in your code:

  • 一个资源只能有一个内容-输入值.因此,您必须决定要使用的媒体类型.我建议 audio/mpeg.
  • 您忘记在内容中指定配置-处置.如果您只想提供文件名而不想更改配置,请使用默认值 inline.

其余看起来不错.但如果找不到文件,我也会发送 404 状态码.

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 提供服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以
PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的
mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)
Laravel Gmail Configuration Error(Laravel Gmail 配置错误)
Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)
Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)