Quantcast
Channel: Knowledge Sharing » PHP
Viewing all articles
Browse latest Browse all 3

Avoid Direct Download

$
0
0

Case study:
We would like to provide pdf files to be downloaded by visitors, but the file location is hidden. Visitors can download file with URL given only. Programming language used is PHP.

Analysis:
We could make a link directly to the file location. Example:

<a href="myebook.pdf">Download MyEbook</a>

What happen if the link above clicked? It depends to the visitors’ browser. If the plugin to read pdf is installed, then browser will open that file inside browser. But, how if there’s no pdf reader plugin? Then a save file dialog box appear. We could save that file.

A problem is done if the visitors don’t have pdf reader plugin. But what about the visitors who have it? Of course the problem is not solved. Another problem is the visitors know the file location,  so it allows the visitors to download the file directly (download it directly by typing/copy paste the URL in the browser, without clicking from link given).

Solution:
We need a bit code (PHP) to solve this problem.

The code to avoid direct download is shown below:

$task = $_REQUEST['task'];
  1.  
  2. switch($task) {
  3.  case 'download':
  4.  // file location
  5.  $file_path = 'dl/myebook.pdf';
  6.  
  7.  // function to get file name without path
  8.  $file_name = basename($file_path);
  9.  
  10.  // get file size
  11.  $fsize = filesize($file_path);
  12.  
  13.  // set headers
  14.  header("Pragma: public");
  15.  header("Expires: 0");
  16.  header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
  17.  header("Cache-Control: public");
  18.  header("Content-Description: File Transfer");
  19.  header("Content-Type: application/pdf");
  20.  header('Content-Disposition: attachment; filename="' . $file_name . '"');
  21.  header("Content-Transfer-Encoding: binary");
  22.  header("Content-Length: " . $fsize);
  23.  
  24.  // start downloading from here
  25.  $file = @fopen($file_path,"rb");
  26.  if ($file) {
  27.    while(!feof($file)) {
  28.    print(fread($file, 1024*8));
  29.    flush();
  30.      if (connection_status()!=0) {
  31.          @fclose($file);
  32.          die();
  33.      }
  34.    }
  35.    @fclose($file);
  36.  }
  37.  
  38.  break;
  39.  default:
  40.    echo '<a href="dlfile.php?task=download">Download MyEbook</a>';
  41.    break;
  42. }

You need to concern to this line:

header('Content-Disposition: attachment; filename="myebook.pdf"');

This line forces browser to show save dialog box for “myebook.pdf” although there’s pdf reader plugin in the browser.

To change file type (exe or zip), we need to change Content-Type only. Here are the lists:

// archives
application/zip
// documents
application/pdf
application/msword
application/vnd.ms-excel
application/vnd.ms-powerpoint
// executables
application/octet-stream
// images
image/gif
image/png
image/jpeg
// audio
audio/mpeg
audio/x-wav
// video
video/mpeg
video/quicktime
video/x-msvideo

File starts downloading from the line after this line:

// start downloading from here

There’s a question. Why do we need to avoid direct download? The purpose is to check how many times a file has been downloaded. If the visitors are allowed to download files directly, I’m sure it will be difficult to know how many times a file is downloaded. If you don’t need to check how many times the visitors download your files, you could use both direct download or indirect download.

Related posts:

  1. Creating Database in MySQL

Viewing all articles
Browse latest Browse all 3

Trending Articles