Serving Large Protected Files in PHP/Apache

我与影子孤独终老i 提交于 2019-12-05 10:30:36
hobbs

The nicest solution in my opinion: install mod_xsendfile in your Apache, have the PHP script authorize the user, and on success send a response with an X-Sendfile header pointing to the location of the protected file. From that point on, Apache does the work of serving the file to the client; not PHP.

What about using symlinks? If you have a folder example:

userfacingfiles/
  md5_of_order_id1 --> protected-file.exe
  md5_of_order_id2 --> protected-file.exe

protectedfiles/
  .htaccess (contains deny from all)
  protected-file.exe

Basic Example:

$salt = 'canttouchthis';

function create_symlink($order_id, $salt, $protected_file) 
{
  $info = pathinfo('protectedfiles/'.$protected_file);

  symlink('protectedfiles/'.$protected_file, 'userfacingfiles/'.md5($order_id.$salt).'.'.$info['extension']);
}


function get_file($order_id, $salt, $extension)
{

  header('Location: userfacingfiles/'.md5($order_id.$salt).'.'.$extension);
  exit();
}

usage:

When the user pays:

create_symlink(1, 'secureSALT', 'ebook.pdf');

When the user wants to download their ebook

get_file(1, 'secureSALT');

This may not be the most portable method, but because you're redirecting the user the web server is handling the downloads.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!