开发者

PHP output internal server file?

开发者 https://www.devze.com 2023-04-06 02:33 出处:网络
On my server I have a directory with music files, generally in .mp3 format. I\'m writing a web application to search for and play these tracks.

On my server I have a directory with music files, generally in .mp3 format. I'm writing a web application to search for and play these tracks.

All the files are stored, with their absolute server path, artist, album and title info in a MySQL database.

What I want to do is have a PHP file that "outputs" an mp3 file on the server that would normally be inaccessible from the web. So, this is what I want to achieve:

client requests play.php?id=10 PHP gets absolute server path from MySQL database where ID = 10 PHP outputs the file (which would really be at e.g. '/home/user/files/no_web/mp3/Thing.mp3') To the client, it looks like there is an mp3 file called http://myserver.com/play.php?id=10 and it starts to play.

I'm sure this sor开发者_高级运维t of thing is possible, just not sure how. Thanks in advance :)


You need to send correct content-type header and then just output the file:

header('Content-type: audio/mpeg3');
readfile('filename.mp3');


For reading the file and sending it, you can use the readfile function.

For setting the mime-type, so the browser actually knows what type of file is sent by the webserver, use the header function like:

header('Content-Type: audio/mpeg');

Additionally, you may also want to set the Content-Length HTTP header.

header('Content-Length: ' . filesize($filepath) );


If all you're trying to do is let the user download the mp3, just use the readfile command which will read the mp3 file and pass it along to the client. However you need to make sure to set the mime-type correctly.

0

精彩评论

暂无评论...
验证码 换一张
取 消