PHP输出内部服务器文件?
在我的服务器上,我有一个包含音乐文件的目录,通常为 .mp3 格式。我正在编写一个网络应用程序来搜索和播放这些曲目。
所有文件及其绝对服务器路径、艺术家、专辑和标题信息都存储在 MySQL 数据库中。
我想要做的是有一个 PHP 文件,可以在服务器上“输出”通常无法从网络访问的 mp3 文件。所以,这就是我想要实现的目标:
客户端请求 play.php?id=10 PHP 从 MySQL 数据库获取绝对服务器路径,其中 ID = 10 PHP 输出文件(实际上位于“/home/user/files/no_web/mp3/Thing.mp3”) 对于客户端来说,看起来有一个名为 http://myserver.com/play 的 mp3 文件。 php?id=10 然后就开始播放了。
我确信这种事情是可能的,只是不确定如何实现。提前致谢 :)
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 sort of thing is possible, just not sure how. Thanks in advance :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您需要发送正确的
content-type
标头,然后输出文件:You need to send correct
content-type
header and then just output the file:要读取文件并发送它,您可以使用 readfile 函数。
要设置 mime-type,以便浏览器实际上知道网络服务器发送的文件类型,请使用 header 函数,例如:
此外,您可能还需要设置 Content-Length HTTP 标头。
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:
Additionally, you may also want to set the Content-Length HTTP header.
如果您只想让用户下载 mp3,只需使用
readfile
命令将读取 mp3 文件并将其传递给客户端。但是您需要确保正确设置 mime-type。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.