1

I have a report that I want to allow users to pull the current data for into a downloadable .csv file.

I am currently using unmanaged_write_submit but this just prints the csv to the screen and users would have to right click and save as. I want to make it as easy as possible, by allowing the actual file to be downloaded through a button click.

$page['submit'] = array(
    '#type' => 'submit',
    '#value' => 'Download Report',
    '#submit' => array('test_unmanaged_write_submit'), 
    );


function test_unmanaged_write_submit($form, &$form_state) {
    //$data is pulling the .csv/query data
    $data = get_array_data($form,&$form_state); 
    $destination = !empty($form_state['values']['destination']) ? $form_state['values']['destination'] : NULL;

    // With the unmanaged file we just get a filename back.
    $filename = file_unmanaged_save_data($data, $destination, FILE_EXISTS_REPLACE);
    if ($filename) {
        $url = file_create_url($filename);
        $_SESSION['file_example_default_file'] = $filename;
        drupal_set_message(t('Saved file as %filename (accessible via !url, uri=<span id="uri">@uri</span>)', array(
       '%filename' => $filename,
       '@uri' => $filename,
       '!url' => l(t('this URL'), $url),
    )));
    }
    else {
        drupal_set_message(t('Failed to save the file'), 'error');
    }
}

But this only provides a link to a webpage that displays the .csv on the screen. Is there another drupal function or way to force a file download in php?

Erik S
  • 13
  • 3

1 Answers1

1

It's been a while since I've used Drupal but based on the answer provided here.

You can set the redirect variable in the $form_state array to the URL of your file. So basically just take the $url from your call to file_create_url and stick it into the redirect:

$form_state['redirect'] = $url;

If that doesn't do the trick you can do it with pure PHP as described here.

tobbr
  • 2,046
  • 3
  • 14
  • 15
  • I managed to get the file to force a download. However, it is downloading as "resource" type and not as csv. Even with my updating the headers in Drupal 7: drupal_add_http_header('Content-type', 'text/csv'); – Erik S Mar 22 '18 at 17:29
  • Which method did you use to force the download? – tobbr Mar 22 '18 at 18:24
  • Setting the redirect variable in $form_state and taking the url – Erik S Mar 22 '18 at 19:39
  • I'm not sure `drupal_add_http_header` persists through redirects, but if it links directly to the .csv and it's served as a resource then that sounds like a web server issue (eg apache/nginx), maybe try the pure php method instead. – tobbr Mar 22 '18 at 20:18
  • The force php method worked, but it gets called without even loading the page. The button callback is where the export_to_csv function is, but it gets called on page load. Do you know why this is? – Erik S Mar 22 '18 at 21:31
  • No I don't know, you put the force download code inside the submit function? If you did and it still behaves like that then i don't know by hand, I have to see your code. – tobbr Mar 22 '18 at 21:56
  • I found a work around for the submit function. However I noticed extensive html code at the end of the .csv . Is there any way to suppress/remove this? I'm not understanding where this is being generated. – Erik S Mar 22 '18 at 21:58
  • Yes, after your force download code, you have to tell Drupal to stop executing, so after you serve the .csv just calling `drupal_exit();` should do the trick. – tobbr Mar 22 '18 at 22:01
  • Thank you very much for your help! Everything works great now. – Erik S Mar 22 '18 at 22:39