简体   繁体   English

从具有.htaccess限制下载的文件夹中播放mp3

[英]playing mp3 from a folder which has .htaccess restriction for download

I have been trying to make a music streaming website. 我一直在尝试制作音乐流媒体网站。

I store all the mp3 in www.abc.com/audio folder . 我将所有mp3存储在www.abc.com/audio文件夹中。

This folder has .htaccess restriction so that none can download any file. 此文件夹具有.htaccess限制,因此没有人可以下载任何文件。

After so much of searching I am unable to find how to play mp3 from this folder using php from this folder. 经过如此多的搜索后,我无法找到如何使用此文件夹中的php从此文件夹中播放mp3。

The PHP code for the play file is filename: play.php 播放文件的PHP代码是文件名:play.php

<?php
//print_r($_GET);
include_once('./functions.php');

$path=getPathFromHash($_GET['value']);   //get path from hash_value ex. www.abc.com/audio/1.mp3
echo $track = getFileNameFromHash($_GET['value']);  //get file name ex. 1.mp3

if (file_exists($track)) {
header("Content-Type: audio/mpeg");
header('Content-Length: ' . filesize($track));
header('Content-Disposition: inline; filename=' . $track);
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";
}


?>
<audio src="<?php echo $path?>" autoplay loop controls></audio>

my .htaccess file in audio folder is 我在音频文件夹中的.htaccess文件是

<FilesMatch ".(ogg|mp3)$">
Order Allow,Deny
Deny from all
</FilesMatch>

But after all this, the song is not playing :( What changes should I make so that song starts to play? 但是毕竟,这首歌没有播放:(我应该进行哪些更改才能开始播放这首歌?

Thank you in advance. 先感谢您。

You have two problems that I can see: 您可以看到两个问题:

1) 1)

echo $track = getFileNameFromHash($_GET['value']);

Remove the echo from this line. 删除此行中的echo This will cause the file name to be output and will at least corrupt the file data, it will probably prevent the headers from being set correctly as well. 这将导致输出文件名,并且至少会破坏文件数据,这也可能会阻止正确设置标头。

2) 2)

You probably need to tell PHP that the files are in the audio/ directory. 您可能需要告诉PHP这些文件在audio/目录中。 You say that getFileNameFromHash() returns ex. 1.mp3 您说getFileNameFromHash()返回ex. 1.mp3 ex. 1.mp3 - well PHP will look for 1.mp3 in the current working directory, which according to your comment above will be the webroot. ex. 1.mp3好吧,PHP会在当前工作目录中查找1.mp3,根据您上面的评论,该目录将是webroot。

Try changing the line mentioned above to 尝试将上述行更改为

$track = 'audio/' . getFileNameFromHash($_GET['value']);

...which will also mean you should change the Content-Disposition line to: ...这也意味着您应该将Content-Disposition行更改为:

header('Content-Disposition: inline; filename="' . basename($track) . '"');

All of this assumes that $path contains a sensible value. 所有这些都假定$path包含一个有意义的值。 It should be /play.php?value=<hash value expected by your functions> 它应该是/play.php?value=<hash value expected by your functions>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM