Loading ...
Sorry, an error occurred while loading the content.

A PHP Downloader Script

Expand Messages
  • Day Dreamer
    Hi friends, If u r a frequent PHP coder, u may need to run some short of download script which automatically starts downloading some file. Ofcourse I am not
    Message 1 of 1 , Oct 31, 2004
    • 0 Attachment
      Hi friends,
      If u r a frequent PHP coder, u may need to run some short of download
      script which automatically starts downloading some file. Ofcourse I am
      not talking about hyperlink. In hyperlink the fatal problem is user
      may know your actual path where the file is located. But in some cases
      you may want your files to be downloaded only by authenticated users,
      this time u must use this downloader script. There is no possibility
      of leeching your file if u use it. Lets take a look at the following
      script

      <?php
      $fileid = $_GET['fileid'];
      if (!empty($fileid))
      {
      switch ($fileid)
      {
      case 1: $filepath = "\PHPExperts1.zip";
      break;
      case 2: $filepath = "\PHPExperts2.zip";
      break;
      case 3: $filepath = "\PHPExperts3.zip";
      break;
      }
      $file = basename($filepath);
      $path = substr($filepath,1);
      header("Cache-Control: ");// leave blank to avoid IE errors
      header("Pragma: ");// leave blank to avoid IE errors
      header("Content-type: application/octet-stream");
      header("Content-Disposition: attachment; filename=\"".$file."\"");
      header("Content-length:".(string)(filesize($path)));
      $fdl=fopen($path,"r");
      fpassthru($fdl);
      }
      else
      echo "You didn't specify any file to download. The correct request
      shoud be<br>";
      echo "http://phpexperts.info/download.php?fileid=id <br> <font color
      = 'red'>where id = 1 or 2 or 3</font>";
      ?>

      In above case I uploaded three files named PHPExperts1.zip,
      PHPExperts3.zip, PHPExperts3.zip at the same path where this script
      resides. Now depending upon users choice, I let them download it. I
      dont implement the basic authentication part but I hope u can do it too.

      A better running example of this script is located at

      http://www.phpexperts.info/download.php?fileid=1
      http://www.phpexperts.info/download.php?fileid=2
      http://www.phpexperts.info/download.php?fileid=3

      Highlighted Code located at :
      http://www.phpexperts.info/code1.php

      Remeber to chmod your files atleast to 0444 otherwise some error will
      occur due to permission settings

      I hope you will enjoy it.

      --August
      Administrator
      BanglaExpress
    Your message has been successfully submitted and would be delivered to recipients shortly.