我可以使用 PHP 提供 MP3 文件吗?

发布于 2024-08-06 09:18:09 字数 577 浏览 2 评论 0原文

就像可以使用 php 提供图像以用于验证码等一样,是否可以对音频文件执行相同的操作?

我已经尝试过,

<?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 控件几秒钟,然后“无视频”。

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

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?

I've tried this

<?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";
}

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

梦晓ヶ微光ヅ倾城 2024-08-13 09:18:09

您的内容处置应该是:

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

但不确定这是否是问题所在。我还建议使用 readfile 来输出文件:

readfile($rSong);

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

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

Your Content-Disposition should be:

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

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

readfile($rSong);

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"); 
沩ん囻菔务 2024-08-13 09:18:09

尝试使用此类 它支持下载恢复和速度限制相信我,作为 mp3 下载网站的所有者,您需要它

try using This Class it supports download resume and speed limit believe me u need it as an owner of mp3 downloads website

你的呼吸 2024-08-13 09:18:09

根据此处的讨论,加上 readfile() 来自 PHP 网站,适用于 PHP 4、5、7 和8、我想出了这个:

if (file_exists($file)) {
  header('Content-Description: File Transfer');
  header("Content-Transfer-Encoding: binary");
  header('Content-Type: audio/mpeg');
  header('Content-Disposition: attachment; filename="'.basename($file).'"');
  header('Expires: 0');
  header('Cache-Control: must-revalidate');
  header('Pragma: public');
  header('Content-Length: '.filesize($file));
  readfile($file);
  exit;
}

Per the discussion here, plus readfile() from the PHP site for PHP 4, 5, 7 & 8, I came up with this:

if (file_exists($file)) {
  header('Content-Description: File Transfer');
  header("Content-Transfer-Encoding: binary");
  header('Content-Type: audio/mpeg');
  header('Content-Disposition: attachment; filename="'.basename($file).'"');
  header('Expires: 0');
  header('Cache-Control: must-revalidate');
  header('Pragma: public');
  header('Content-Length: '.filesize($file));
  readfile($file);
  exit;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文