5

I am working on a site where the admin wants a set of tabular data. I created a module to display this data in the Control Panel, no problems there. My first concern was if there is a preferred method for storing module specific files (a CSV in this case). I settled on adding a directory within the module that stores the CSVs. Is there a better way? Particularly more secure?

My next issue was in forcing the download of the CSV. I would like to use the proper URL structure to hit a download_csv method in my module, set the headers and read out the file. When I do this, the outputted CSV has a full page markup added to the bottom. Here is the code I used in download_csv:

header("Content-Type: application/csv");
header("Content-Disposition: attachment; filename=$filename");
header("Pragma: no-cache");
readfile($filedir.$filename);

Again is there a way to use the proper URL structure and the download_csv method without getting all the "junk" added to the file?

Thanks!

3

It's a matter of preference where you store the generated CSVs, but one argument against storing them in the module is that it may make it easier to overwrite them inadvertently when updating the module. I'd use a config setting that points to the directory and leave it up to the user.

To your second point, I've had good success generating CSVs using the following helpers/libs:

Good luck!

| improve this answer | |
  • Fantastic, thank you! I am keeping the files in there for now since it is just a one-time module, but will be considering other options in the future. The helpers worked perfectly though. I ended up with this for my download_csv method: $this->EE->load->helper('download'); $this->EE->load->helper('file'); $filedata = read_file($filedir.$filename); force_download($filename,$filedata); – 2bsharpdev Jan 22 '13 at 21:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.