• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Counting Downloads and display to user

#1
[eluser]Drayen[/eluser]
Hi guys, I know this is probably relatively simple but I've had a lot of trouble trying to do this.

Let's say my view is view.php and my controller is controller.php. The index would display some dynamic files and I want the downloads for each one to be shown next to the download link. I also need a dynamic redirect since there will be multiple pages.

What's the best way to go about this? I'd prefer if the user never even left the page and if the download is clicked it would simply just add +1 to the downloads without even having to direct them somewhere else and send them back.

The way I had it was the download links pointed to a download function in the same controller, but upon clicking it would always take me to a white page and I wasn't sure if even post variables can be passed through a normal download link or required form submission.

Thanks for any help guys.

#2
[eluser]Mirage[/eluser]
In order to count downloads server side you must use PHP to return the file, setting your own headers and returning the file with e.g. readfile().

Another way is to use Javascript. You could make an AJAX call with the file url which increases a counter and then redirect to the actual url. If you won't want the page to reload, you can target the link/redirect at a hidden iframe.

HTH,
-m

#3
[eluser]Drayen[/eluser]
[quote author="Mirage" date="1221444484"]In order to count downloads server side you must use PHP to return the file, setting your own headers and returning the file with e.g. readfile().

Another way is to use Javascript. You could make an AJAX call with the file url which increases a counter and then redirect to the actual url. If you won't want the page to reload, you can target the link/redirect at a hidden iframe.

HTH,
-m[/quote]

Yea I know I need to use PHP to do this, but what's an easy way in conjunction with mysql?

Is there an example some where I can use as reference? I've tried searching. I know lots of sites do something like this. The User Guide is extremely helpful but some of the parameters described are not very clear to me.

Like here:

http://ellislab.com/codeigniter/user-gui...tml#update

I'm not sure how the parameters are involved for the part not involving an array/object.

So basically I would send the user to download.php,

update the file downloads in the table where the download id corresponds to the id sent with download.php (so do I need to use $_POST for this and do I need a form to use post?) I have no clue how to do this syntax in CodeIgniter.

Thanks for the help.

#4
[eluser]Mirage[/eluser]
You don't need a post for this - you could use url params:

Code:
http://somesite.com/download/fileid

Incrementing a file counter in the database could be as easy as

Code:
UPDATE download_counts SET num_downloads=num_downloads+1 where file_id={$file_id}

Or you could simply 'log' the downloaded file to get you some more meaningful statistics

Code:
INSERT INTO download_log(dl_time, file_size, file_name) VALUES (......);

Then you can use aggregate COUNT functions, etc. etc to get the number of downloads, number of downloads for a given period and so on.

HTH,
-m

#5
[eluser]Drayen[/eluser]
[quote author="Mirage" date="1221548523"]You don't need a post for this - you could use url params:

Code:
http://somesite.com/download/fileid

Incrementing a file counter in the database could be as easy as

Code:
UPDATE download_counts SET num_downloads=num_downloads+1 where file_id={$file_id}

Or you could simply 'log' the downloaded file to get you some more meaningful statistics

Code:
INSERT INTO download_log(dl_time, file_size, file_name) VALUES (......);

Then you can use aggregate COUNT functions, etc. etc to get the number of downloads, number of downloads for a given period and so on.

HTH,
-m[/quote]


Got it 100% working, thanks for the help!

#6
[eluser]Bramme[/eluser]
Would you mind sharing the code? I might have to create something similar in the future... I'm very interested.

#7
[eluser]Drayen[/eluser]
[quote author="Bramme" date="1221570948"]Would you mind sharing the code? I might have to create something similar in the future... I'm very interested.[/quote]

Code:
$this->load->helper('download');
    $id = $this->uri->segment(3);
    $data = file_get_contents("../ugh/$id.w3g");
    $name = "[sitename]$id.txt";
    $this->db->query("UPDATE table SET Downloads=Downloads+1 where Id='$id'");
    force_download($name, $data);

I used it as a function in the same controller so no redirect was necessary, it kept me on the same page and the download just popped up.


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2019 MyBB Group.