Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Php download code could be optimised plus needs a change

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

Tech or Treat! - Giveaway

Submit an article about your scariest tech experience—and the solution—and you’ll be automatically entered to win one of 4 fantastic tech gadgets.

Question has a verified solution.

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

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…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to dynamically set the form action using jQuery.
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

636 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