artdarek/force-download

Force File Download for PHP

1.0.1 2014-12-12 06:17 UTC

This package is not auto-updated.

Last update: 2024-04-13 13:25:41 UTC


README

If on your page you place a link to file like "pdf" browser will automaticly open it, but what if you don't want that file to be opened, instead you just want user to be able to download this file. If so this package is a solution for you :D

Installation

Add artdarek/force-download to your composer.json file:

"require": {
  "artdarek/force-download": "dev-master"
}

Use composer to install this package.

$ composer update

Usage

Create file called ie. 'download.php'.

Include artdarek/force-download library:

  // include composer autoloader 
  require 'ForceDownload.php';

or if you are using composer autoloader include this instead:

  // include composer autoloader 
  require 'vendor/autoload.php';

after this simply add:

  // initialize download
  $force = new Artdarek\ForceDownload();
  $force->download();

you can also pass your own config to allow only some file types to be downloadable:

	// custom config
	$config = array(
    'allowed_extensions' => array(
      // archives
          'zip' => 'application/zip',
      // documents
          'pdf' => 'application/pdf',
      // images
          'gif' => 'image/gif',
          'png' => 'image/png',
          'jpg' => 'image/jpeg',
          'jpeg'=> 'image/jpeg',
    )
	);

  // initialize download
  $force = new Artdarek\ForceDownload($config);
  $force->download();

Done... now you can create a url that will directly start downloding specified file instead of opening it in the browser.

  <a href="download.php?dir=download_folder&file=example.pdf">Download</a>

See examples directory for more usage examples :D