?
Solved

Php download code could be optimised plus needs a change

Posted on 2011-03-24
2
Medium Priority
?
261 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 2000 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

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

Question has a verified solution.

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

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
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.
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…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses
Course of the Month14 days, 20 hours left to enroll

840 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