javascript - Prompt to save a PHP dynamically created file to local computer -


i looking way results search generates file can downloaded , saves user computer. able generate file, however, gets stored on server automatically rather prompting user store it.

in search form user can select checkbox generate csv file of output (i prefer have download button on results page unable figure out how that): https://www.evernote.com/shard/s68/sh/54e95567-d91e-4649-a5c1-35b5f8929c13/a4bc4fed2a2c7f7801099f6d5711c49e

then in next page results shown on page , appropriate file generated.

code generates file on results page:

if($print_flag == 1) {   $filename = "exportfile" . date("y-m-d_h_m_s") . ".csv";   $handle = fopen($filename, 'w+');   fputcsv($handle, array($column1,$column2));   fclose($handle); } 

$print_flag set if user checks csv checkbox in prior form.

this creates file stored on server rather allowing user save locally.

i saw previous questions php save file users computer not figure out how add headers or whether right approach.

thanks!

most browsers use mime-type headers reason.

generally, calls set headers should possible. imperative go before echo statements, , there nothing outside <?php ?> block goes before them.

you need put 2 lines code, again possible:

header('content-type: text/csv'); 

this tells browser csv file, , sets types of files filtered in browser's save-as dialog.

header("content-disposition: attachment;filename=$name.csv"); 

tells browser offer save file. depending on browser, can either pop full save dialog, or more commonly, save file default downloads folder called $name.csv. double-quotes allow filename included setting $name.


Comments

Popular posts from this blog

css - Which browser returns the correct result for getBoundingClientRect of an SVG element? -

gcc - Calling fftR4() in c from assembly -

.htaccess - Matching full URL in RewriteCond -