在 PHP 中阻止对某些 .mp3 文件的访问
我想只允许注册用户下载 .mp3 文件。
因此,我决定隐藏 .mp3 文件的实际位置并允许使用 http://example 下载.com/donwload.php?mp3_name=1
文件 donwload.php 检查用户是否登录,然后使用 readfile() 读取未向用户显示的文件夹中的文件位置。
这里的问题是,访问 http://example.com/donwload.php?mp3_name=1< /a> 对于 500kb 的 .mp3 文件需要 3 秒才能加载(出现保存对话框)
是否有其他方法可以这样做或在 PHP 中快速读取文件?
谢谢 阿卡什
I want to allow only registered user's to download a .mp3 file.
So, I decided to hide the actual location of the .mp3 file and allow downloads using http://example.com/donwload.php?mp3_name=1
The File donwload.php checks weather the user is logged in and then uses readfile() to read the file location in a folder that is not shown the the user.
The problem here is that, accessing http://example.com/donwload.php?mp3_name=1 for a .mp3 file of 500kb takes 3 seconds to load (the save dialog box to appear)
Is there any other way to do so or read files quickly in PHP?
Thanks
Akash
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
答案很简短,因为我已经用完了。 (稍后将提供更多信息)。这个答案还对您想要做的事情采取了另一种方法。
如果可能,您应该使用具有专用 ACL 的系统来支持存储在其上的所有文件。
例如,如果您使用 Amazon S3,那么您可以为存储在存储桶中的每个对象提供自己的 ACL,并且还可以动态生成仅在 X 分钟内有效且签名的链接。
考虑到您的情况,您可以做的是将您拥有的每个 MP3 文件存储在 Amazon S3 等设备上(还有其他文件,所以不必使用 S3),然后当用户进行购买时,交易确认后,您可以使用 S3 API 生成图像的链接。
它类似于:
get_object_url( 'my-mp3s.com', 'albums/Foo/bar.mpg')
然后您将获得一个可以提供给客户的 URL。
或者,您可以要求亚马逊生成一个在 15 分钟内过期的 URL。
Really short answer, as I'm running out. (Will provide more information later). This answer also takes an alterntive approach to what you are trying to do.
If possible you should use a system that has a dedicated ACL backing all of the files which are stored on it.
For instance, if you go with Amazon S3, then you can provide your own ACL for each object that is stored within a bucket, and you can also generate links on the fly that are valid and signed for only X number of minutes.
Given your scenario, what you could do is store every MP3 file that you have on something like Amazon S3 (There are others out there so don't feel like you have to use S3), and then when a user makes a purchase and the transaction is confirmed, you can use the S3 API to generate a link for the image.
It would be something like :
get_object_url( 'my-mp3s.com', 'albums/Foo/bar.mpg')
You will then get a URL which you can provide to the customer.
Alternatively, you can ask Amazon to generate a URL that expired within 15 minutes.
您可以使用 file_get_contents 而不是 readfile() http://php.net/ Manual/en/function.file-get-contents.php
或 X-Sendfile apache 模块。
You could use file_get_contents instead of readfile() http://php.net/manual/en/function.file-get-contents.php
or the X-Sendfile apache module.
如果您的 mp3 位于不同的服务器上,则可以使用 modsecdownload 安装 lighttpd,或者如果您只有一台计算机,则可以在不同的端口上运行 lighttpd
http://redmine.lighttpd.net/wiki/1/Docs:ModSecDownload
这样您就可以仅为登录用户生成下载链接,该链接有效期为几分钟至几小时,他们可以下载。我认为这更好,因为如果流量很大,它会比读取文件并将其发送给用户使用更少的内存。
希望这有帮助!
If you have your mp3 on a different server you could install lighttpd with modsecdownload or run lighttpd on a different port if you have only one machine only
http://redmine.lighttpd.net/wiki/1/Docs:ModSecDownload
so you than you could generate only for logged in users a download link, which is valid for few mins - hours, and they can download it. I think this is better, cause it will use less memory than reading the file and sending it to the user, if you have much traffic.
Hope this helps!