0

I had been using PHPExcel but switched to PHP_xlsxwriter because of speed. I am really satisfied with the performance but it only works on local server somehow.

$filename = "Report.xls";

$writer->writeToFile($filename);

if (file_exists($filename)) {
    header('Content-Description: File Transfer');
    header('Content-Type: application/octet-stream');
    header('Content-Disposition: attachment; filename="'.basename($filename).'"');
    header('Expires: 0');
    header('Cache-Control: must-revalidate');
    header('Pragma: public');
    header('Content-Length: ' . filesize($filename));
    readfile($filename);
    unlink($filename);
    exit;
} 

This is the code and it works completely fine but on server file_exists($filename) does not catch the condition. And after seeing that I don't even know how it worked on local. How did file_exists find the file in local server? It does not even have a specific path.

Do I need to specify the path on the server? Could anyone give me an example or an answer to solve this problem? Thanks!

Min Lee
  • 347
  • 1
  • 3
  • 8
  • Where is your project located? /var/www/html/xls_project/ ? you may need to specify the full path or the base path. – unixmiah Sep 21 '18 at 20:19
  • @unixmiah I tried but I am not sure which path I need to put. I tried putting my project folder but didn't work. I'm not even sure where `$writer->writeToFile($filename)` is stored before it gets in to `if (file_exists($filename))` – Min Lee Sep 21 '18 at 20:34
  • you script must error out, do you have the error, it will at least complain about path issue or permission issue. – unixmiah Sep 21 '18 at 20:35
  • @unixmiah It doesn't really print out error message because it isn't really an error.. `file_exists` just returns false because it doesn't find the file so it doesn't go into the if statement. – Min Lee Sep 21 '18 at 20:41
  • try this $filename = dirname(__FILE__) . '/Report.xls'; – unixmiah Sep 21 '18 at 20:45
  • @unixmiah do I need to change 'FILE' to something else? I get an error saying `Use of undefined constant FILE - assumed 'FILE' in ...` – Min Lee Sep 21 '18 at 20:49
  • so you've tried file_exists("/full/path/to/project/Filename.xls") ? and that didn't work? – unixmiah Sep 21 '18 at 20:52
  • @unixmiah I did `$filename = dirname(__FILE__) . "Report.xls";` and the error message is gone, but still not working. It really is weird it works perfect in local server. – Min Lee Sep 21 '18 at 20:52
  • @unixmiah That's the problem. I am not sure where the file is stored. You mentioned `full/path/to/project/` but `$writer->writeToFile($filename);` doesn't download the file so I don't know where the file is. If I knew I would try the full path but have no idea. It should go into the `if` statement in order to download. – Min Lee Sep 21 '18 at 20:55
  • this is to test, give the file chmod 777 and add to the web group, so if you're on ubuntu its www-data:www-data and give it another try – unixmiah Sep 21 '18 at 20:58
  • @unixmiah Hey unixmiah. I really appreciate that you have spent time with me. I finally got it working somehow. I manually put the xls file to the path and ran it and it worked. After that I removed the file and it still is working. I have no idea how it could solve the problem but it did. I thank you again really. Have wonderful weekends ! :) – Min Lee Sep 21 '18 at 21:06

1 Answers1

0

I also had the same problem. The error message I got is like this: "Error: ZipArchive class does not exist\n, referer: http://yoursite..." It happened because I did not have ZipArchive class installed.

See this page for installation instructions. Install it and I hope it will work.