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
Solved

Php download code could be optimised plus needs a change

Posted on 2011-03-24
2
252 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
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
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 …

809 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