Solved

Php download code could be optimised plus needs a change

Posted on 2011-03-24
2
249 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
2 Comments
 
LVL 2

Accepted Solution

by:
wrightsonm earned 500 total points
Comment Utility
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
Comment Utility
Exactly what I needed, thanks for your fast reply
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Consider the following scenario: You are working on a website and make something great - something that lets the server work with information submitted by your users. This could be anything, from a simple guestbook to a e-Money solution. But what…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
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.

762 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now