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

[eluser]peter222[/eluser]
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:

file:
Code:
/home/myprofile/uploads/pict1.jpg

anchor:
Code:
<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:
Code:
path: /home/myprofile/uploads/
file: pict1.jpg
hash: 23f3b4c5df6ce6cc6434565759826734
and then
anchor:
Code:
<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?
#2

[eluser]danmontgomery[/eluser]
http://ellislab.com/codeigniter/user-gui...elper.html

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);
#3

[eluser]peter222[/eluser]
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:

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

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

[eluser]peter222[/eluser]
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!
#6

[eluser]peter222[/eluser]
Finally my working index function in download controller looks like this:

example link:

Code:
<a href="http://mysite.com/download/index/folder/subfolder/subfolder/strangefilename@#$.jpg">strangefilename@#$.jpg</a>

index function code:

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