Search code examples
phpmp3

Block access to certain .mp3 files in 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


Solution

  • 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.