Welcome Guest, Not a member yet? Register   Sign In
Download site structure help
#1

[eluser]HooJee[/eluser]
Hi Guys

I am working on a website whereby users can download files. I am in a dilemma in how to structure the controllers/DB layout for the download URLs. At the moment I have the download URLs hardcoded into the db e.g. /download/file/100
Is this a good way of approaching this issue ? if anyone has a better way of handling this issue or a good design pattern then please share.
#2

[eluser]atw[/eluser]
I'm no expert but I wouldn't hardcode anything I didn't absolutely-definitely-certainly-irrefutably have to.

It would generally be preferable to store only the filename in the DB and create some form of config file which holds the path to that file. At least you then only need to change the path in ONE PLACE rather than for EVERY ENTRY in the whole DB.

If you need category style control over files, add a category storage column or table to the database, then use that to decide which path to use from your config file.

Example config.php
Code:
$file_paths['mp3'] = 'path/to/mp3s/';
$file_paths['img'] = 'path/to/imgs/';
$file_paths['torrent'] = 'path/to/torrents/';

Example controller.php
Code:
if($result['category'] == mp3) {
  send_download($file_paths['mp3'] . $result['filename_from_db']);
}
if($result['category'] == img) {
  send_download($file_paths['img'] . $result['filename_from_db']);
}
if($result['category'] == torrent) {
  send_download($file_paths['torrent'] . $result['filename_from_db']);
}

Disclaimer:
It has taken me 3 months to get to beginner level in CodeIgniter (but I'm reasonable in raw PHP). So I may be talking complete c3ap and that is only pseudo-code, so you could make it MUCH better Smile
#3

[eluser]Yorick Peterse[/eluser]
You should store any URL's in the database at all, unless they are external URL's. In this case you'd want your URL's to look like the following :

Code:
// http://www.yourwebsite.com/index.php/class_name/function/file_id
http://www.yourwebsite.com/index.php/downloads/file/54

Then in the controller you would fetch the DB row based on the ID that's given in the URL.




Theme © iAndrew 2016 - Forum software by © MyBB