-2

I'm using PHPExcel to generate an excel file and I need to save it on a specific server folder by calling my PHP file with ajax (to avoid the extra window open).

I've found this post and the second answer have something close to what I need, but that downloads the file through the browser and I don't want that. I just want to save the .xls file in a specific folder of the server, not in my computer.

This is what I have:

PHP:

$objWriter = new PHPExcel_Writer_Excel5($objPHPExcel);
$objWriter->save('../pdf-totext/'.$_POST["folder"].'/'.$_POST["filename"]); //save the file here

JS:

 $.ajax({
  type:'POST',
  url:'php/libs/PHPExcel-1.8/F29.php',
  data: {folder:folder,filename:filename}
});

So it's possible to save a PHPExcel generated .xls file, calling my PHP file through AJAX?

EDIT

I'm ok with the server and client side thing, it's just I want to call my PHP file through ajax, so my PHP file saves the .xls on a server folder.

EDIT 2

It works if I move to another window like this (and with GET instead POST in the PHP file):

window.open('php/libs/PHPExcel-1.8/F29.php?folder='+folder+'&filename='+filename,'_blank');

But if It doesn't work if I call the PHP file through AJAX.

  • AJAX makes HTTP requests, it doesn’t “save” anything. _“but that download the file through the browser and I don't want that”_ - then what _do_ you want? A script that downloads a file to the website visitor’s computer, without even asking them? That is of course not possible. – 04FS Feb 12 '19 at 08:06
  • @04FS I've editted the entire post and please see my **EDIT** too – Roberto Sepúlveda Bravo Feb 12 '19 at 11:32
  • So what actual problem still needs solving then? Your PHP code already contains the call to `$objWriter->save`, which will store the file into the given location. Return a different response to your AJAX call, if you are not happy with the current one. – 04FS Feb 12 '19 at 11:35
  • @04FS The problem is that PHPExcel is not working if I call my PHP file through AJAX, it's just not saving the file. – Roberto Sepúlveda Bravo Feb 12 '19 at 11:37
  • **How** is it not working? Do you get errors? Did it not create a file? What have you done so far to try and debug this? – 04FS Feb 12 '19 at 11:39
  • @04FS It doesn't throws errors, the .xls is just not being generated, please see my **EDIT 2** – Roberto Sepúlveda Bravo Feb 12 '19 at 11:45
  • That sounds like the parameters where not send correctly when using POST. Have you verified what $_POST contains in that case? – 04FS Feb 12 '19 at 11:54
  • Yes, that contains the route and it works as I say in my **EDIT 2** – Roberto Sepúlveda Bravo Feb 12 '19 at 12:02
  • Your Edit 2 was talking about switching the whole thing over to GET … that is not what I asked you about! – 04FS Feb 12 '19 at 12:04
  • @04FS I've verified and the parameters are right. – Roberto Sepúlveda Bravo Feb 12 '19 at 12:37
  • The `safe` method should throw an exception if it can’t write the file … did you get any of those? – 04FS Feb 12 '19 at 12:39
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/188276/discussion-between-roberto-sepulveda-bravo-and-04fs). – Roberto Sepúlveda Bravo Feb 12 '19 at 12:40

1 Answers1

1

It's PHPExcel... PHP doesn't run on the client, it runs on the server - so it would save the file to the server by default... AJAX is for sending messages from javascript (the client browser) up to the server (received/processed by PHP code), and receiving data back.

You write PHPExcel code in PHP, so you are already on the PHP (webserver) side, so no need for javascript/AJAX - unless you want to allow the user to click a button and request that the PHPExcel-generated file be downloaded from the server to the client.

By the way, PHP doesn't have access to the local file system, so you can't tell the webserver where to save the file on the local computer. PHP will save the file onto the webserver.

However, if your webserver is local (eg xampp or mamp or some such), then that is a different story. Let us know if that is what you are doing. (You still wouldn't use AJAX to save the file - but PHP would have access to the local drive because the webserver is ON the local drive)

cssyphus
  • 37,875
  • 18
  • 96
  • 111
  • yep, I'm using Xampp – Roberto Sepúlveda Bravo Feb 12 '19 at 04:34
  • I've editted the entire post and please see my **EDIT** too. – Roberto Sepúlveda Bravo Feb 12 '19 at 11:35
  • First question: I don't understand - it is a PHP file that creates the PHPExcel spreadsheet - why do you need ajax to communicate? *OR* do you mean that the PHPExcel process is launched via a web page and you need the web page to *also* tell the PHP file where to save the file? – cssyphus Feb 13 '19 at 20:33
  • A second question: When you said `I just want to save the .xls file in a specific folder of the server, not in my computer.` do you mean that the `.xls` file should be saved into a specific subfolder underneath the `xampp/htdocs` folder *(since that folder IS the webserver in xampp)*. – cssyphus Feb 13 '19 at 20:35