Data export to excel & save directly to the direct

2019-08-09 15:45发布

问题:

Currently on clicking on export user is prompt a box to save/open the export excel file to their system , though what i want is when user click on export/send data , the data get exported & save to the server on specific folder & later which I mail that file to the user as an attachment.

Did I have to make any change in my header for the same ?

For ref below is the header from my export excel library:

  header("Content-type: application/vnd.ms-excel"); 
  header("Content-Disposition: attachment; filename=$this->filename"); 
  header("Pragma: no-cache"); 
  header("Expires: 0"); 
  print "$header\n$data"; 

Thanks in advance.

回答1:

The only thing you really need to do is save the Excel contents to disk on your server, you can also remove those headers completely as they are only needed when outputting the data to the browser.

When you mail the user, simply attach the file and send.

It should be pretty straight forward.

EDIT:

To save the excel file:

file_put_contents('<your file name>', '<your file contents>');

The content can be any string, so if you have the excel file contents in a variable, simple put it there.

The file name can be a relative or absolute path.