1

I'm trying to generate an excel file and directly save it on my web server (PHP) with this code:

$content = '"ColHeader1"'."\t".'"ColHeader2"'."\t".'"ColHeader3"'."\t"
  . "\n"
  . '"lorem ipsum"'."\t".'"dolor sit amet"'."\t".'"consectetur lorat"'."\t"."\n"
  . '"pew pew pew"'."\t".'"gun shot"'."\t".'"and Bang!!"'."\t"."\n"
  ;

$filedir = './assets/doc/';
$filename = date('Y-m-d-H-i-s');
$filepath = realpath($filedir . $filename . '.xls');

header("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename='".$filepath."'");
header("Pragma: no-cache");
header("Expires: 0");

echo $content . "\n";

the problems I need help with are:

  • the offered filename doesn't have any file extension even though I already put .xls in the $filepath
  • it only creates the file on my local computer instead of the server

can anyone help me with these? can those 2 points actually be done?

I've read How to save and generate excel file and save in server but I have no idea what to do with it or how to implement it to my code.

I've also read Generate Excel file, but do not save to the server but if possible, I'd like to use just pure PHP..

dapidmini
  • 1,490
  • 2
  • 23
  • 46
  • I found out that `file_put_contents()` does exactly these 2 things. so silly of me. I think this short comment is the answer.. should I use the Answer Your Question button instead? – dapidmini Feb 19 '21 at 18:10

0 Answers0