2 weeks ago

Laravel secure bin files download with limitations

Posted 2 weeks ago by domantas

Currently I working on secure bin file download. All my bin files are store in Storage/app/files folder. I need to track that the user can download only 5 files in 24 hours. If the user downloaded 5 files per 24 hours, the next time he can download a file are the next day (after 24 hours). Any help will be very useful.

 * $id {string} = file name.
public function downloadFile(Request $request, $id){
      $file = storage_path('app/files/') . $id .'.bin';
      if (file_exists($file)) {
        $user = new User;
        $downloadHistory = new DumpDownloadHistory;

          'user_id' => auth()->user()->id,
          'dataset' => $id,
          'user_ip'=> request()->ip()

          $headers = [
              'Content-Type' => 'application/bin'

        $response = response()->download($file, $id.'.bin', $headers, 'inline');
        if ($response) {
          return $response;
      } else {
          abort(404, 'File not found!');

This is my database of download history:

     * user_id {int} = User id.
     * dataset {string} = file name.
     * user_ip {string} = user ip (User ip address from where the user has downloaded the file)
    public function up()
        Schema::create('dump_download_histories', function (Blueprint $table) {


Please sign in or create an account to participate in this conversation.