CodeIgniter Forums
Sending file path to controller - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Sending file path to controller (/showthread.php?tid=8872)



Sending file path to controller - El Forum - 06-03-2008

[eluser]Loque[/eluser]
Hi all,

I have done some searching (probably not looking up the right terms) and i really cant find any information on sending filenames, or varialbes containing '/' to controllers. At the moment I would like to use the force download function, and have an individual controller to handle this, so something super basic like;

Code:
function download($s_name,$s_file)
{
    force_download($s_name,$s_file);
}

and say my link to this would be something like;

www.theSite.com/controllerName/download/filename.jpg/file/path/here/is/never/going/to/work/filename.jpg

I cant think of a way of getting around this other than stripping the slashes and then putting them back in, and that just feels wrong - someone please slap with common sense and help me if you have a mo!

Cheers,
Loque


Sending file path to controller - El Forum - 06-03-2008

[eluser]stuffradio[/eluser]
These two links should help you out:
http://ellislab.com/codeigniter/user-guide/helpers/url_helper.html
http://ellislab.com/codeigniter/user-guide/general/routing.html


Sending file path to controller - El Forum - 06-03-2008

[eluser]minimal design[/eluser]
The way I do it on my site goes something like that:

site.com/downloads/get_file/2

where "2" is the id of the file in the DB.

All my downloads are stored in the same directory so in your case you might have to store path of file in DB too.

Code:
function get_file($id) {
    // you get the file name + path + whatever info I need from the DB.
    // The model will vary a lot depending on setup so you'll have to figure that part...
    $f = $this->downloads_model->get_file_info($id);
    // then you download the file like that
    $name = $f['file_name']; // name of downloaded file
    $f_uri = '/downloads/'.$name;
    $file = file_get_contents($f_uri); // need to read file's contents to pass to force_download()
    force_download($name, $file);
}

You should also check out the new file helper... Haven't played with it yet, but it might make things even simpler.

EDIT:

here's what the model could look like, to give you a basic idea:


Code:
function get_file_info($id) {
        $this->db->select('file_name, file_path, download_count');
        $query = $this->db->get_where($this->dl_table, array('id' => $id), 1);
        if ($query->num_rows() == 1) {
            $file = $query->row_array();
        }
        return $file;
    }



Sending file path to controller - El Forum - 06-03-2008

[eluser]Loque[/eluser]
Thanks for the quick reply,

I have read these before (and just again now), I am a big fan of the CI documentation but these ones really had me stuck (probably just my lack of understanding). I'm guessing your really suggesting i need to do something with routing... but then reading it this doesnt seem like its what I am after:

Quote:In some instances, however, you may want to remap this relationship so that a different class/function can be called instead of the one corresponding to the URL.

I dont want to call another class/function I would just like a way of chucking a URL as a var to the class/function.

I must be missing something really basic here =`]


Sending file path to controller - El Forum - 06-03-2008

[eluser]Loque[/eluser]
Big thanks minimal, you guys are fast =`] guna check now and see if i can get this working!


Sending file path to controller - El Forum - 06-03-2008

[eluser]minimal design[/eluser]
[quote author="Loque" date="1212554383"]Thanks for the quick reply,

I have read these before (and just again now), I am a big fan of the CI documentation but these ones really had me stuck (probably just my lack of understanding). I'm guessing your really suggesting i need to do something with routing... but then reading it this doesnt seem like its what I am after:

Quote:In some instances, however, you may want to remap this relationship so that a different class/function can be called instead of the one corresponding to the URL.

I dont want to call another class/function I would just like a way of chucking a URL as a var to the class/function.

I must be missing something really basic here =`][/quote]

check the URI class:
Code:
$this->uri->uri_string()

but I think "site.com/get_file/id" scheme is way cleaner personally...


Sending file path to controller - El Forum - 06-03-2008

[eluser]Loque[/eluser]
Oki yea reading more, the path is completely coming from an HTML page (which is basically a JS pimped file browser), the idea is that you can upload files via FTP and give someone a link to this page with image/movie preview functionality and can click a download button to then download the file... thinking on the spot it might be easier just to make a single PHP file and do this the pre-CI way.

Thanks again guys, please do post if theres an answer, I'm guessing this '/' topic is why people keep on asking about using get queries.

EDIT

Yea minimal I totally agree about doing this with a DB, typical to have a one off... still curious about sending variables to a controller with '/' in however.

EDIT again..

Yea think there may actually be a way with $this->uri->uri_string()