Browsing: PHP Download File Script – Free Must Have Class

PHP File download class will be helpful for developers and users to provide facility of “File Download” by hiding real file paths. Chip File Download Class which is written in PHP is very flexible and easy to use. The script triggers the download dialogue so that visitors can choose to download the file instead of opening it in the browser.

This is very useful PHP download script. You can use PHP file download script to avoid direct files downloads (hide real file paths), and to limit downloads to only some set of file types. Get free Demos of Framework and Zend PHP.

It is an easy to use force file download script, useful for sites that have different files and would like force people to download the files instead of having them run remotely off their web server. This can be applied to mp3′s, video clips, archives, docs, images, pdf files and more.

Features

  • Provide any file type for download
  • Flexible control over files type, which are being offered to download
  • A set of predefined mime type settings for most common file extensions
  • Built-in feature for detecting mime type automatically, if Mimetype or Fileinfo extension installed on the server
  • Hot-linking protection
  • Displays download dialogue
  • Download folder protection
  • Directory(s) containing files, within “download_path” support

Requirements

  • PHP5 or higher

Extension Requirements – Optional

  • Mimetype Extension
  • Fileinfo Extension

I have already given a full sample code in the download archive but i will also explain the usage of class online as simple as possible. I hope you will be able to use it within a little time.

Include class.chip_download.php

You have to include download file script in your file at the suitable position you think. I will recommend to use all code of this tutorial at the top of your page to avoid any output warning.

require_once("class.chip_download.php");

Variable: $download_path

Set $download_path variable. You should provide absolute path to the download directory. Windows example can be “E:/wamp/www/tutorialchip/download/”, where as Linux example can be “/home/tutorialchip/public_html/download/”.

Variable: $file

Set $file variable. You should provide file name in this variable. Example can be “nature.zip”, “nature.jpg” etc.

Array: $args

  • download_path (String – Absolute Path): It will hold value of $download_path.
  • file (String – Valid File Name): It will hold value of $file.
  • extension_check (boolean TRUE | FALSE): It is TRUE by default, It will give you a flexible control to offer particular files to download with this PHP download script. You can set it to FALSE, if you donot want to control over file types and willing to download all types of files.
  • referrer_check (boolean TRUE | FALSE): It is FALSE by default, It will provide you a flexible control over hot-linking. You have to set “referrer” after making it TRUE. It will allow download files from a server which will match “referrer” value only.
  • referrer (String): You have to set its value after setting “referrer_check” TRUE. An example of its value can be “tutorialchip.com” so download will only be offered from tutorialchip server request only.

Class Instance

It is time to instance PHP download file script class now.

$download_path = "E:/wamp/www/tutorialchip/download/";
$file = $_REQUEST['f'];

$args = array(
		'download_path'		=>	$download_path,
		'file'				=>	$file,
		'extension_check'	=>	TRUE,
		'referrer_check'	=>	FALSE,
		'referrer'			=>	NULL,
		);
$download = new chip_download( $args );

Pre Download Hook

PHP download class will do its job, and provide you a Hook to execute your code or changes before throwing download stream to the users. You can check available options of Pre Download Hook by executing following code.

$download_hook = $download->get_download_hook();
//$download->chip_print($download_hook);
//exit;

You can remove comments of last two lines to stop the execution of code and check the available options. A sample output of successful $download_hook array can be,

Array
(
    [file_path] => E:/wamp/www/tutorialchip/download/nature.jpg
    [file] => nature.jpg
    [file_name] => nature
    [file_extension] => jpg
    [file_size] => 97854
    [file_mime_type] => image/jpeg; charset=binary
    [download] => 1
    [message] => File is ready to download
)

A sample output of non successful $download_hook array can be,

Array
(
    [file_path] => E:/wamp/www/tutorialchip/downloads/nature.jpg
    [download] =>
    [message] => Invalid File or File Path.
)

Download key of the $download_hook array will be TRUE in case of successful execution and FALSE in case of any error.

Download Initialization

It is the last step of PHP download file script settings. You can make the best use of Download key of the $download_hook array now.

if( $download_hook['download'] == TRUE ) {

	/* You can write your logic before proceeding to download */

	/* Let's download file */
	$download->get_download();

}

Complete Tutorial – PHP File Download Example

I have already explained the usage of PHP file download class in steps. Now it is time to see the whole tutorial at once.

<?php
/*
|-----------------
| Chip Download Class
|------------------
*/

require_once("class.chip_download.php");

/*
|-----------------
| Class Instance
|------------------
*/

$download_path = "E:/wamp/www/tutorialchip/download/";
$file = $_REQUEST['f'];

$args = array(
		'download_path'		=>	$download_path,
		'file'				=>	$file,
		'extension_check'	=>	TRUE,
		'referrer_check'	=>	FALSE,
		'referrer'			=>	NULL,
		);
$download = new chip_download( $args );

/*
|-----------------
| Pre Download Hook
|------------------
*/

$download_hook = $download->get_download_hook();
//$download->chip_print($download_hook);
//exit;

/*
|-----------------
| Download
|------------------
*/

if( $download_hook['download'] == TRUE ) {

	/* You can write your logic before proceeding to download */

	/* Let's download file */
	$download->get_download();

}

?>

I hope – this tutorial will be helpful for you to understand the usage of class. I am using this class to offer tutorial downloads to the tutorialchip users.

Screenshot

Online Demo

I have developed online demo of this script. I hope you will enjoy.

I hope – this tutorial will be helpful for you to understand the usage of class. I am using this class to offer tutorial downloads to the tutorialchip users.

License – GPLv2

This class has been distributed under GPLv2.

Download Package: 1.1 – 399 Kb

Click here to download complete package of this class (version 1.1). Package will contain,

  • class.chip_download.php – Main Class
  • download.php – Sample download script
  • index.php – Demos home page
  • style.css – Minimal style
  • download directory – Sample download files

Download Package: 1.0 – 305 Kb

Click here to download complete package of this class (version 1.0).

1.1

Class is updated on December 23, 2010.

  • Download support for files which are within directory(s) under “download_path”

1.0

Class is released on November 29, 2010.

  • This is the first version of PHP file download class
email