PHP How to allow users to download a file from server

PHP How to allow users to download a file from server

Introduction

In this article, we will learn How to allow users to download a file from the server? 

Demo Project File

Index.php: To display options for file download.
downloadFile.php: It will process the file download, in case you need more control on Who will allow downloading.
PHP_DOWNLOAD_FILE_FROM_SERVER.ZIP: Our Sample file which we need to download from the server

Scenario 1: Direct Link for the file.

It is one of the simplest examples to allow a user to download a file which is available on our web server. Everyone can download the files. 

Following is the code for index.php in which we are allowed end users to download our sample file.

Code

<p>
	Scenario 1: Direct Link for the file.
	<br/>
	<a href="PHP_DOWNLOAD_FILE_FROM_SERVER.ZIP"> Download File </a>
</p>

Output

Scenario 2: Control who is allowed downloading the file or Conditionally allow the download.

In this scenario, we need to control when an end user can download the appropriate file. You can use this scenario when

  • You want to disable the direct file access.
  • You want to allow only logged-in users to download the files.
  • You want to check some conditions before you want to allow the file to download.

For this scenario, we added another link in our index.php file. Following is the final code for the index.php file.

Code

<p>
	Scenario 1: Direct Link for the file.
	<br/>
	<a href="PHP_DOWNLOAD_FILE_FROM_SERVER.ZIP"> Download File </a>
</p>
<p>
	Scenario 2: Control who is allowing to download the file or Conditionally allow download.
	<br/>
	<a href="downloadFile.php?ID=123"> Download File </a>
</p>

For the second link, we are redirecting the user to the downloadFile.php page with an ID. Here “ID” may be a file ID or a token ID to download any specific file. 
Next, on downloadFile.php we have code to validate the provided ID. We also have a code to check if the user is authenticated or not. The authentication code is with some dummy values. We are not checking session here. It is an example How you can add other logic. Finally, if all the conditions are true, we are allowing file download. Following is the code for downloadFile.php.

Code

<?php

$FileID=-1;
if(isset($_GET["ID"]))
{
	$FileID	= intval($_GET["ID"]);
}
$Message="<br/>Invalid File ID.";
if($FileID > 0)
{
		if($FileID==123)
		{
				$Message="<br/>Please login to download the File.";
				/* Add your logic to check user login status */
				$IsUserLogin=false;
				if($IsUserLogin)
				{
					$Message="<br/>File is not available.";
					$file="PHP_DOWNLOAD_FILE_FROM_SERVER.ZIP";
					if (file_exists($file)) {
						$Message="";
						header('Content-Description: File Transfer');
						header('Content-Type: application/octet-stream');
						header('Content-Disposition: attachment; filename='.basename($file));
						header('Expires: 0');
						header('Cache-Control: must-revalidate');
						header('Pragma: public');
						header('Content-Length: ' . filesize($file));
						readfile($file);
						exit;
					}
				}
		}
}
if(strlen($Message)>0)
{
	echo "PHP File Download Example.";
	echo $Message;
	echo "<br/> <a href='index.php'>Go Back to Home</a> ";
}

Output with IsUserLogin=false

Output with IsUserLogin=true

0 Comments
Leave A Comment

Please login to post your valuable comments.

Join the newsletter

Get the latest vLemonn news first

share