Solved

Php download code could be optimised plus needs a change

Posted on 2011-03-24
2
256 Views
Last Modified: 2012-05-11
Hi

My code provides downloads, it is not optimized, that is the first issue, and the second is, that I want it to start downloading straight away, instead of just providing another link which needs to be clicked

I get around 20 downloads a day at that site, so there is no need for a backup, but i want to track the downloads, while not requiring the user to click another time is that possible?

As the code is right now

They click
The file  they download gets logged
Actual download link is shown
They click it

But at some sites, for sharing software, they only accept direct links, so then i give them something like

http://www.123.com/directdownload.zip

I would want to guve them a link which logs on my server the type of file, date and still gives them that download

Can this be done with my code?


<?php 

$LocationOfLogFile = "/logs/downloads.txt" ;

$file = $_GET['file'] ;
$type = $_GET['typefile'] ;
if ( $_GET['file'] == !"" )
{
   if ( $_GET['typefile'] == !"" )
   {
      $URLofDownloadableFile = "$file" ;
      print "<a title=\"Download $title_clean\" href=\"$type/$file.zip\" class=\"value\" target=\"_self\"><b>download</b></a> $title_clean " ;
      print "<br>" ;
      include ( "incl/downloads.php" ) ;
      $ShowLast = "Last Download: " . date( "M d Y H:i:s", fileatime( "incl/lastdownload.txt" ) ) ;
      $fp = fopen( "incl/lastdownload.txt", "w+" ) ;
      fwrite( $fp, $ShowLast ) ;
      fclose( $fp ) ;

      $LocationOfLogFile = $_SERVER['DOCUMENT_ROOT'] . $LocationOfLogFile;
      $filehandle = fopen( $LocationOfLogFile, 'a' ) ;
      if ( $filehandle )
      {
         $line = date( 'M d Y H:i:s' ) . "\t" ;
         $line .= $title_file . "<br>\n" ;
         fwrite( $filehandle, $line ) ;
         fclose( $filehandle ) ;
      }
  }
}
?>

Open in new window

0
Comment
Question by:PeterdeB
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 2

Accepted Solution

by:
wrightsonm earned 500 total points
ID: 35211381
Your code you have looks ok for logging the download.  What you need to do is append something to grab the requested file and serve it to the user.  The code as shown below will do just that.  In the browser window, a save as popup will open.

$_GET['file'] = urldecode($_GET['file']);

$file = /abs-path-to-root-directory/'.$_GET['file'];

if(!isset($_GET['id']) || $_GET['id']==''){
  echo"<p><strong>File Not Found</strong></p>";
}
else if(file_exists($file)){
  $ext = substr(strrchr($file, '.'), 1);
    
  switch($ext){

  case 'pdf':
    header("content-type: application/pdf");
    break;
  case 'PDF':
    header("content-type: application/pdf");
    break; 
  default:
    header("Content-type: application/force-download");
    header("Content-Transfer-Encoding: Binary");
    header("Content-disposition: attachment; filename=\"".basename($file)."\"");   
  }
  header("Content-length: ".filesize($file));
  ob_end_clean();
  readfile_chunked($file,true);
  exit;

  
  }
else {echo"<p><strong>File Not Found</strong></p>";  }

  function return_bytes($val) {
    $val = trim($val);
    $last = strtolower($val[strlen($val)-1]);
    switch($last) {
        // The 'G' modifier is available since PHP 5.1.0
        case 'g':
            $val *= 1024;
        case 'm':
            $val *= 1024;
        case 'k':
            $val *= 1024;
    }

    return $val;
}

function readfile_chunked($filename,$retbytes=true) {
   $chunksize = 1*(1024*1024); // how many bytes per chunk
   $buffer = '';
   $cnt =0;
   // $handle = fopen($filename, 'rb');
   $handle = fopen($filename, 'rb');
   if ($handle === false) {
       return false;
   }
   while (!feof($handle)) {
       $buffer = fread($handle, $chunksize);
       echo $buffer;
       ob_flush();
       flush();
       if ($retbytes) {
           $cnt += strlen($buffer);
       }
   }
       $status = fclose($handle);
   if ($retbytes && $status) {
       return $cnt; // return num. bytes delivered like readfile() does.
   }
   return $status;

} 

Open in new window

0
 

Author Closing Comment

by:PeterdeB
ID: 35252250
Exactly what I needed, thanks for your fast reply
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question