Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 12-04-2012, 01:46 AM   PM User | #1
rpcob
New to the CF scene

 
Join Date: Aug 2011
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
rpcob is an unknown quantity at this point
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.
rpcob is offline   Reply With Quote
Old 12-04-2012, 04:08 AM   PM User | #2
mOrloff
Regular Coder

 
mOrloff's Avatar
 
Join Date: Nov 2008
Location: The Great Pacific NW, USA
Posts: 421
Thanks: 8
Thanked 6 Times in 6 Posts
mOrloff is an unknown quantity at this point
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.
__________________
...because it's dundant already.

Last edited by mOrloff; 12-05-2012 at 03:56 PM..
mOrloff is offline   Reply With Quote
Old 12-05-2012, 12:55 AM   PM User | #3
rpcob
New to the CF scene

 
Join Date: Aug 2011
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
rpcob is an unknown quantity at this point
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.
rpcob is offline   Reply With Quote
Old 12-05-2012, 12:58 AM   PM User | #4
Redcoder
Regular Coder

 
Redcoder's Avatar
 
Join Date: May 2012
Location: /dev/couch
Posts: 309
Thanks: 2
Thanked 46 Times in 45 Posts
Redcoder has a little shameless behaviour in the past
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.
__________________
For professional Hosting and Web design.....


NetEssentials.co.uk
Redcoder is offline   Reply With Quote
Old 12-05-2012, 01:50 AM   PM User | #5
rpcob
New to the CF scene

 
Join Date: Aug 2011
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
rpcob is an unknown quantity at this point
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
rpcob is offline   Reply With Quote
Old 12-05-2012, 09:49 AM   PM User | #6
Redcoder
Regular Coder

 
Redcoder's Avatar
 
Join Date: May 2012
Location: /dev/couch
Posts: 309
Thanks: 2
Thanked 46 Times in 45 Posts
Redcoder has a little shameless behaviour in the past
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.
__________________
For professional Hosting and Web design.....


NetEssentials.co.uk
Redcoder is offline   Reply With Quote
Old 12-06-2012, 12:54 AM   PM User | #7
rpcob
New to the CF scene

 
Join Date: Aug 2011
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
rpcob is an unknown quantity at this point
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..
rpcob is offline   Reply With Quote
Old 12-08-2012, 05:12 AM   PM User | #8
rpcob
New to the CF scene

 
Join Date: Aug 2011
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
rpcob is an unknown quantity at this point
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..
rpcob is offline   Reply With Quote
Reply

Bookmarks

Tags
count, counter, php, sql

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 07:24 AM.


Advertisement
Log in to turn off these ads.