fuelphp download csv on button click?

300 Views Asked by At

Similar to the uploading feature in fuelphp (link provided below), is there a tutorial for downloading files in fuelphp. There is not much information out there for fuelphp (other than the docs). Would I require a separate config page called download.php similar to upload.php?

All I really need is a page with either a download link or button to export csv to a user's local machine

Link to Upload feature

https://www.tutorialspoint.com/fuelphp/fuelphp_file_uploading.htm

Thanks in advance

1

There are 1 best solutions below

0
steadweb On

Have you looked at the File class? This has a method of download which you pass the path of the file to. The 2nd param allows you to specify the name of the file that's downloaded.

File::download('path/to/file.txt, 'new-file-name.txt');

If you want to restrict downloads per user, you'll need to add logic around that.

https://fuelphp.com/docs/classes/file/usage.html#/method_download

Example

Create a new controller, as you've pointed out, download.php, and use the following as a starting point.

You'll need to pass something through to the get_index in order to determine which file the client will download. I'd suggest some sort of look up, using an unique identifier, instead of a file path, otherwise this would easily be exploited.

class Controller_Download extends Controller 
{ 
    public function get_index()
    {
        // @todo add logic surrounding file download
        File::download('path/to/file.txt, 'new-file-name.txt');
    }
}