Recently, I am finding a good solution to protect the uploaded media files against non authorized downloading. I am especially concerned about downloading media file direct access through browser and hotlink prevention. The uploaded media files (mp3 files) stored in 'media' directory under document root in my server.
Observations:
To prevent hotlink prevention and downloading media files direct access through browser can be controlled using .htaccess file under the 'media' directory. That is,
(a) The user cannot download the media files directly from the browser like http://www.domain.com/media/mj_heal_the_world.mp3
(b) The prevention of bandwidth theft or "hotlink" (direct linking to a web site's files)
Now the embed player would be working without any trouble. Similarly, you can able make sure the required tweaks for flash player and downloading media files to the authenticated users.(a) The user cannot download the media files directly from the browser like http://www.domain.com/media/mj_heal_the_world.mp3
(b) The prevention of bandwidth theft or "hotlink" (direct linking to a web site's files)
-> . htaccess
<FilesMatch "\.(mp3|MP3)$">
order deny,allow
deny from all
</FilesMatch>
The above code worked well for prevention of hotlink and restriction of downloading media file directly from the browser. But the following functionalities were not working due to the above code implementation.
(a) The embed player does not play mp3 files
(b) The flash player does not play mp3 files.
(c) The authenticated users not able to download the mp3 files
Please note that my website have both embed player and flash player for playing mp3 files. Since the both player source code pointed to the actual mp3 files location and due to above .htaccess code the player failed to play the appropriate mp3 files.
How to avoid conflict (as explained above) and make sure the player functions and keep up the .htaccess? - Here I have given a solution for embed player.
Initially, the embed code looks like below:
-> embed.php
<?php
<embed src="http://www.domain.com/media/mj_heal_the_world.mp3">
?>
The above embed player does not work due to above explained .htaccess code implementation. Now this HTML code should be changed in to little bit and pointed to a intermediate PHP script that would get the filename as a parameter and then read the file content immediately. This solution is another way of serving content to the user.
Finally the embed.php code should be:
-> embed.php
<?php<embed src="http://www.domain.com/play.php?filename=mj_heal_the_world.mp3">?>
-> play.php
<?php# get file name$fileName = $_GET['filename']# assemble file path$file = '/var/www/domain.com/public_html/media/'.$fileName;$fileSize=filesize($file);
header("Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0");header("Pragma: no-cache");header("Content-Type: audio/mpeg");header("Content-Disposition: attachment; filename=\"" . $fileName . "\"");header("Content-Length: " . $fileSize);readfile ($file);?>
0 comments:
Post a Comment