Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    New to the CF scene
    Join Date
    Aug 2011
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Add to Download Count Click Counter

    Hello,
    I have been using a script that uses a counter on the download page when it is visited. It will add 1 to the "dcount" table in my sql for the corresponding file id.
    PHP Code:
    addToDownloadCount($fid); 
    I have removed the download page and added direct links to the display page of the file. Is there a way to simply have the code activate when the download link is clicked?

    Thank you in advanced.

  • #2
    Regular Coder mOrloff's Avatar
    Join Date
    Nov 2008
    Location
    The Great Pacific NW, USA
    Posts
    422
    Thanks
    8
    Thanked 6 Times in 6 Posts
    Are you locked into a PHP-only solution for some reason??
    I only ask because this seems like a great opportunity for JavaScript to show off a little.
    I'm sure someone more experienced than myself could come up with other (and possibly more elegant??) solutions, but my thought is to leave them as simple links on the page you serve.

    Then, use JS to override their default behavior and make an AJAX call to a separate PHP script that serves up the file & does whatever other background stuff you might have in mind.

    I have some code clips that can at least get you rolling ... but they're on my dev machine at the shop.
    Let me know if there are any constraints you are working under, and I'll try to remember to check back in tomorrow morning (cross your fingers & hold your breath ).

    At a bare minimum, I hope this at least gives you some additional Google fodder.
    Last edited by mOrloff; 12-05-2012 at 03:56 PM.
    ...because it's dundant already.

  • #3
    New to the CF scene
    Join Date
    Aug 2011
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Im not stuck on php however my problem with js is sometimes it doesn't translate well on mobile devices.

    Any code and directions you could give me would be great. As of right now on the display.php I just have the download counter being used as a page counter and below it a direct link to the file being called.

    Thank you for the help.

  • #4
    Regular Coder Redcoder's Avatar
    Join Date
    May 2012
    Location
    /dev/null
    Posts
    333
    Thanks
    2
    Thanked 47 Times in 46 Posts
    You can pass the direct link to another PHP script via GET that will have the logging code.

    E.g http://yourdomain.com/downloads_logg....com/ebook.pdf

    The downloads logger can then send the download by headers -header() Check php.net manual after logging the download and counting.

  • #5
    New to the CF scene
    Join Date
    Aug 2011
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Redcoder View Post
    You can pass the direct link to another PHP script via GET that will have the logging code.

    E.g http://yourdomain.com/downloads_logg....com/ebook.pdf

    The downloads logger can then send the download by headers -header() Check php.net manual after logging the download and counting.
    What would the link look like for that?

    Im a little familiar with GET command. Ive used it once or twice before. Im already using one on the display page to get the file name and display it as the webpage title. I just don't know where to start for this

  • #6
    Regular Coder Redcoder's Avatar
    Join Date
    May 2012
    Location
    /dev/null
    Posts
    333
    Thanks
    2
    Thanked 47 Times in 46 Posts
    Quote Originally Posted by rpcob View Post
    What would the link look like for that?

    Im a little familiar with GET command. Ive used it once or twice before. Im already using one on the display page to get the file name and display it as the webpage title. I just don't know where to start for this
    Check out $_GET contents of url, like $_GET['url'] and you'll have the url of the file, then you parse the URL to get the name of the specific file. Icrement it and use headers to 'push' the download to the users browsers.

  • #7
    New to the CF scene
    Join Date
    Aug 2011
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Redcoder View Post
    Check out $_GET contents of url, like $_GET['url'] and you'll have the url of the file, then you parse the URL to get the name of the specific file. Icrement it and use headers to 'push' the download to the users browsers.
    How do I do this without sending them to another page as well as get the headers and counter added included?

    PHP Code:
    <?php
    $varFile 
    $_GET["http://url.com/files/'.$filename.'"];
    ?>
    My url already looks like: http://url.com/display.php?nav=display&file=4959
    Last edited by rpcob; 12-06-2012 at 12:58 AM.

  • #8
    New to the CF scene
    Join Date
    Aug 2011
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I've tried three different attempts that both dont seem to work

    First: Returns a page full of garbage like if you opened a zip file in notepad (.zip file)
    PHP Code:
    $fid $_GET['file'];
    $results mysql_query("SELECT filename FROM files WHERE id=$fid");
            if (
    mysql_numrows($results) == 0){
                echo 
    "<b>File not found</b>";
                return;
            }
    $file mysql_result($results,0,"filename");
    $fileurl 'http://url.com/files/'.$file;

         
    // Set headers
         
    header("Cache-Control: public");
         
    header("Content-Description: File Transfer");
         
    header("Content-Disposition: attachment; filename=$fileurl");
         
    header("Content-Type: application/zip");
         
    header("Content-Transfer-Encoding: binary");
        
         
    // Read the file from disk
         
    readfile($fileurl);
    exit(); 
    Second: Returns same garbage (.zip file)
    PHP Code:
    <?PHP
    $fid 
    $_GET['file'];
    $results mysql_query("SELECT filename FROM files WHERE id=$fid");
            if (
    mysql_numrows($results) == 0){
                echo 
    "<b>File not found</b>";
                return;
            }
    $file mysql_result($results,0,"filename");
    $fileurl 'http://url.com/files/'.$file;

    header('Content-Description: File Transfer');
    header('Content-Type: application/zip');
    header('Content-Disposition: attachment; filename="'.basename($fileurl).'"'); //<<< Note the " " surrounding the file name
    header('Content-Transfer-Encoding: binary');
    header('Connection: Keep-Alive');
    header('Expires: 0');
    header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
    header('Pragma: public');
    header('Content-Length: ' filesize($fileurl));
    readfile($fileurl);
    ?>
    Third: Returns same garbage, but also alters page layout
    PHP Code:
    $fid $_GET["file"];
    $results mysql_query("SELECT filename FROM files WHERE id=$fid");
            if (
    mysql_numrows($results) == 0){
                echo 
    '<b>File not found</b>';
                return;
            }
    $file mysql_result($results,0,'filename');
    $fileurl "http://url.com/files/".$file;
        
    // Inform browser that this is a force-download
        
    header('Content-Description: File Transfer');
        
    header('Content-Type: application/octet-stream');
        
    // Inform browser that data can be binary in addition to text
        
    header('Content-Disposition: attachment; filename='.basename($fileurl));
        
    header('Content-Transfer-Encoding: binary');
        
    // Inform browser that this page expires immediately so that an update to the file will still work.
        
    header('Expires: 0');
        
    header('Cache-Control: must-revalidate');
        
    header('Pragma: public');
        
    header('Content-Length: ' filesize($fileurl));
        
    // Push actual file.
        
    ob_clean();
        
    flush();
        
    readfile($fileurl);
        exit(); 
    On a side note there are both .zip, .rar, and .txt files. Do i just add content type lines?
    Last edited by rpcob; 12-08-2012 at 05:40 AM.


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •