运行 PHP 直接访问 mp3 文件之前的脚本
Running a PHP script prior to direct access to an mp3 file
我正在尝试将有关直接访问此服务器上的 mp3 文件(例如通过 iTunes)的统计信息添加到 Google 分析。我想 运行 这个 PHP 函数并像没有重定向一样传送文件。
其中的 mod_rewrite 部分似乎运行良好。我的现场下载脚本和流媒体播放器可以在不触发规则的情况下访问 mp3 文件。
RewriteCond %{HTTP_REFERER} !^http://(www\.)?mysite\.com [NC]
RewriteRule ^audio/episodes/([^/\.]+).mp3$ /audio/episodes/google_analytics_mp3.php?mp3=&redirected=1 [L,QSA]
然而,我最初尝试在 PHP 脚本 运行s 之后传送 mp3 并没有传送直接访问的文件。只是浏览器出现白屏,iTunes 出现错误。
if ($id = intval($_REQUEST['mp3'])) {
$e = new Episode;
list($ep) = $e->retrieve("id = $id");
if ($ep) {
ga_send_pageview(basename($ep->audio_link), $ep->google_title());
if ($_GET['redirected'] == 1) {
$fileLocation = '/'.$ep->audio_link;
header("Location: $fileLocation");
exit();
}
}
}
还尝试在位置 header 之前发送 header('Content-Type: audio/mpeg');
。同样的结果。
我的第二次尝试使浏览器下载文件作为附件,这不适用于 iTunes 等程序,这不是我想要的结果。
if ($_GET['redirected'] == 1) {
$fileName = basename($ep->audio_link);
$fileLocation = $_SERVER['DOCUMENT_ROOT'].'/audio/episodes/'.$filename;
header("Content-Transfer-Encoding: binary");
header("Content-Type: audio/mpeg, audio/x-mpeg, audio/x-mpeg-3, audio/mpeg3");
header('Content-length: ' . filesize($fileLocation));
header('Content-Disposition: attachment; filename="'.$fileName.'"');
header('X-Pad: avoid browser bug');
header('Cache-Control: no-cache');
readfile($fileLocation);
exit();
}
更新: Per Martin 的建议也尝试了第二种方法,但处置设置为内联 header('Content-Disposition: inline; filename="'.$fileName.'"');
但 Chrome 仅显示白屏。如果我禁用从等式 Chrome 中删除脚本的规则,将显示本机 mp3 播放器和播放文件。
如何在 PHP 脚本 运行s 之后简单地允许正常访问 mp3 文件?
我之前遇到过 Content-length:
的问题,您可以尝试注释掉这一行。我认为文件系统文件大小 reader 与浏览器文件大小量词不同,因此浏览器期望(并获得)99% 的文件显然不完整,因此它无法输出文件,因为它认为文件不完整数据。
File System File Size as read !== browser file size
不过我不确定为什么会这样。
我正在尝试将有关直接访问此服务器上的 mp3 文件(例如通过 iTunes)的统计信息添加到 Google 分析。我想 运行 这个 PHP 函数并像没有重定向一样传送文件。
其中的 mod_rewrite 部分似乎运行良好。我的现场下载脚本和流媒体播放器可以在不触发规则的情况下访问 mp3 文件。
RewriteCond %{HTTP_REFERER} !^http://(www\.)?mysite\.com [NC]
RewriteRule ^audio/episodes/([^/\.]+).mp3$ /audio/episodes/google_analytics_mp3.php?mp3=&redirected=1 [L,QSA]
然而,我最初尝试在 PHP 脚本 运行s 之后传送 mp3 并没有传送直接访问的文件。只是浏览器出现白屏,iTunes 出现错误。
if ($id = intval($_REQUEST['mp3'])) {
$e = new Episode;
list($ep) = $e->retrieve("id = $id");
if ($ep) {
ga_send_pageview(basename($ep->audio_link), $ep->google_title());
if ($_GET['redirected'] == 1) {
$fileLocation = '/'.$ep->audio_link;
header("Location: $fileLocation");
exit();
}
}
}
还尝试在位置 header 之前发送 header('Content-Type: audio/mpeg');
。同样的结果。
我的第二次尝试使浏览器下载文件作为附件,这不适用于 iTunes 等程序,这不是我想要的结果。
if ($_GET['redirected'] == 1) {
$fileName = basename($ep->audio_link);
$fileLocation = $_SERVER['DOCUMENT_ROOT'].'/audio/episodes/'.$filename;
header("Content-Transfer-Encoding: binary");
header("Content-Type: audio/mpeg, audio/x-mpeg, audio/x-mpeg-3, audio/mpeg3");
header('Content-length: ' . filesize($fileLocation));
header('Content-Disposition: attachment; filename="'.$fileName.'"');
header('X-Pad: avoid browser bug');
header('Cache-Control: no-cache');
readfile($fileLocation);
exit();
}
更新: Per Martin 的建议也尝试了第二种方法,但处置设置为内联 header('Content-Disposition: inline; filename="'.$fileName.'"');
但 Chrome 仅显示白屏。如果我禁用从等式 Chrome 中删除脚本的规则,将显示本机 mp3 播放器和播放文件。
如何在 PHP 脚本 运行s 之后简单地允许正常访问 mp3 文件?
我之前遇到过 Content-length:
的问题,您可以尝试注释掉这一行。我认为文件系统文件大小 reader 与浏览器文件大小量词不同,因此浏览器期望(并获得)99% 的文件显然不完整,因此它无法输出文件,因为它认为文件不完整数据。
File System File Size as read !== browser file size
不过我不确定为什么会这样。