Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Allow logged in user to Download File in PHP else nobody can't

Tags:

php

I have .mp3 files in my website and I want to set my site so that after my users have logged in they can download files. If users are not logged in they won't be able to download files. I do not want anyone to be able to find the path of the files.

like image 717
kingjohn Avatar asked Apr 28 '11 03:04

kingjohn


1 Answers

I'd make the file impossible to access via an HTTP request alone, and with PHP, just print it out:

<?php
session_start();
if (isset($_SESSION['logged_in'])) {
  $file = '/this/is/the/path/file.mp3';

  header('Content-type: audio/mpeg');
  header('Content-length: ' . filesize($file));
  readfile($file);
}
?>
like image 149
Blender Avatar answered Sep 20 '22 21:09

Blender