Generate file path for created file so it can be downloaded
/tmp is outside of webroot so apache (or whatever) can't serve files from there.

You have 2 options:


Move the file from /tmp to your public files directory. This will mean anyone will have access to it (if they know the url). See file_unmanaged_move().


Eg.

$source = '/tmp/my_file.csv';
$destination = 'public://csv/my_file.csv';
file_unmanaged_move($source, $destination);



Stream the file to the browser via php. This means only people who can access your php script can get access to the file.


Eg.

header('Content-type: text/csv');
header('Content-disposition: attachment;filename=my_file.csv');
return readfile('/tmp/my_file.csv');