php most memory efficient way to return files

Posted by bumperbox on Stack Overflow See other posts from Stack Overflow or by bumperbox
Published on 2010-06-09T21:50:04Z Indexed on 2010/06/09 21:52 UTC
Read the original article Hit count: 178

Filed under:
|

so i have a bunch of files, some can be up to 30-40mb and i want to use php to handle security of the files, so i can control who has access to them

that means i have a script sort of like this rough example

$has_permission = check_database_for_permission($user, filename);

if ($has_permission) {
   header('Content-Type: image/jpeg'); 
   readfile ($filename);    
   exit; 
} else {
  // return 401 error
}

i would hate for every request to load the full file into memory, as it would soon chew up all the memory on my server with a few simultaneous requests

so a couple of questions

  1. is readfile the most memory efficient way of doing this?
  2. is there some better method of achieving the same outcome, that i am overlooking?

server: apache/php5

thanks

© Stack Overflow or respective owner

Related posts about php

Related posts about apache