Forum Moderators: coopster

Message Too Old, No Replies

Protecting media files

         

madk

5:14 pm on Oct 19, 2008 (gmt 0)

10+ Year Member



I need some help trying to protect media files. These files will range from small jpegs and large jpegs (3 megs) to large movies files (500 megs).

The site I am developing is an industry only site that requires a login and this media will only be viewable to certain authorized users. The media actually doesn't need to be viewable but it needs to be available to download through the browser.

What methods could I use to design a system like this? Server is running apache and site is being built with php/mysql. Any advice will be appreciated.

Thanks!

madk

6:13 pm on Oct 19, 2008 (gmt 0)

10+ Year Member



I think I can safely store my image files outside of root. My only concern is how the server will affect images that are 2-5 megs in size. I believe this method will load the file into ram first and then deliver it to the browser. Anyone have any experience with this?


<?php
session_start();
/*
// Check for permissions here
*/
$path = str_replace('..', '', $_SERVER['QUERY_STRING']);
$fullpath = '/home/site/media/' . $path;
if(!is_file($fullpath)) {
exit('Image not found');
}
header('Content-type:image/jpg');
readfile($fullpath);
?>

ag_47

6:39 pm on Oct 19, 2008 (gmt 0)

10+ Year Member



Yeah, I think you can read it chunk-by-chunk and output is as you go along if you don't want the script hogging too much resources.

// Open file
$file = fopen($path, 'rb');
if(!$file){
// error message
exit;
}

$chunk = 1024*1024; //whatever you want
header('Content-type:image/jpg');

// Push file
while (!feof($file)) {
echo fread($file, $chunk);
flush();
}
fclose($file);

Much more info at PHP.NET

Little_G

7:11 pm on Oct 19, 2008 (gmt 0)

10+ Year Member



Hi,

readfile itself won't read the file to memory but it will end up there anyway if you have output buffering on. You can call ob_end_flush [php.net] just before readfile, to flush and stop buffering.

Andrew