Welcome Guest, Not a member yet? Register   Sign In
file explorer and force_download

I just listed files from my directory and want to add anchor to all of them. I want to use force_download to download this files but don't know how to send path info to download function. For example:


<a href="&lt;?=base_url()?&gt;/download/index/[here_path_and_file]">pict1.jpg</a>

This way will not work, so how to use force_download here?
I know that I can put any random data to database when upload file and do it this way:

database record:
path: /home/myprofile/uploads/
file: pict1.jpg
hash: 23f3b4c5df6ce6cc6434565759826734
and then
<a href="&lt;?=base_url()?&gt;/download/index/23f3b4c5df6ce6cc6434565759826734">pict1.jpg</a>

but is it only this way possible? I realy need database to realise it?


Quote:force_download('filename', 'data')

Generates server headers which force data to be downloaded to your desktop. Useful with file downloads. The first parameter is the name you want the downloaded file to be named, the second parameter is the file data. Example:
$data = 'Here is some text!';
$name = 'mytext.txt';

force_download($name, $data);

If you want to download an existing file from your server you'll need to read the file into a string:
$data = file_get_contents("/path/to/photo.jpg"); // Read the file's contents
$name = 'myphoto.jpg';

force_download($name, $data);

I know this help, but my question is, how can i send info about path and file to my function, maybe I don't understand something but I can't do it this way:

<a href="&lt;?=base_url()?&gt;/download_controller/download_function/[path_and_file]">pict1.jpg</a>

for example:
echo anchor('controller/download/folder/filename', 'title="pict1.jpg"');
&lt;?=anchor('controller/download/folder/filename', 'title="pict1.jpg"')?&gt;
controller or model:
$path = $this->uri->segment(2);
$file = $this->uri->segment(3);
does that help you?

OK I understand now. I couldn't imagine how to handle subdirectories in this case because every subdirectory mean another function argument in URL. But I found uri_string() in url helper so it will be easy to get full path now. Thank You for help!

Finally my working index function in download controller looks like this:

example link:

<a href="http://mysite.com/download/index/folder/subfolder/subfolder/[email protected]#$.jpg">[email protected]#$.jpg</a>

index function code:

$data = file_get_contents(rtrim($this->config->item('myuploadfolder'), '/').str_replace("download/index/","",rawurldecode(uri_string())));
$name = $this->uri->segment($this->uri->total_segments());
force_download(rawurldecode($name), $data);

Theme © iAndrew 2016 - Forum software by © MyBB