Welcome Guest, Not a member yet? Register   Sign In
Access an uploaded file in CI [RESOLVED]
#1

[eluser]kirualex[/eluser]
Hi,

I'm currently working on a project which requires the possibility for the user to upload a file and access it (with verification).
So I managed to have a working upload, but I'm stuck on the matter of accessing it. Indeed, when I put the complete adress, CI try to trigger a controller, when it should just access the correct folder...

I now this may sound stupid but I'm fairly new to CI and this is driving me nuts!
#2

[eluser]mddd[/eluser]
If you want to have direct access to the files, check the .htaccess file to make sure it doesn't rewrite those urls.
E.g. if you store the files in a folder called 'files', you would make a RedirectCond to check if the url doesn't start with /files.

Another solution (safer!) is to store the files outside of the web root entirely. And then use a controller to access them.
E.g. www.example.com/files/id-of-the-file/secret-word would load that file from its location and output it to the user.
(Secret word can also be exchanged for any authentication system, of course).
#3

[eluser]kirualex[/eluser]
Thanks for your response mddd, your second solution (using a controller to access files) seems very interesting, but I have one more question:

How do I launch the download of a specified file from the controller? Is there a dedicated CI function with this purpose?
#4

[eluser]mddd[/eluser]
To send a file as a download, you can simply use the php header() function. For instance if you send a pdf file, use
Code:
header('Content-type: application/pdf');
header('Content-Disposition: attachment; filename="downloaded.pdf"');
The second line forces the file to be saved, with the filename provided. This gives you the option to have a url like /files/id-of-the-file, but still have the file saved with the right name when the user downloads it.

To send the file, simple output it (after putting the headers) like so:
Code:
readfile('/file/on/my/server');
#5

[eluser]LuckyFella73[/eluser]
You can use the CI download helper as well:
http://ellislab.com/codeigniter/user-gui...elper.html

Only when your files are too big you have to do it like mddd posted due to (different)
memory limits of webservers.
#6

[eluser]mddd[/eluser]
Good tip. I had been writing those headers so often, I didn't even look in CI. But you are right, the download functions are even easier Smile




Theme © iAndrew 2016 - Forum software by © MyBB