0

I've got to generate and post a xls file to a server via curl. I just get my data from db, make the first version of the file and save it on my server, then read it and post it via curl to the remote server.

Apparently I'm having some issue with the validation of that file on the remote server which script read an header column that actually doesn't exists.

Opening the file with Microsoft Excel, simply pressing save and closing it fix the problem but is not a approachable option.

Can you suggest something different? This is my code generating the file

$dataToExport = $this->db->getAll($query);
$savePath = '/path/to/my/server';
$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();
$sheet->fromArray($dataToExport, NULL, 'A1');
$sheet->setTitle("Foglio1");
$writer = new Xls($spreadsheet);
$writer->save($savePath);
$cFile = curl_file_create($savePath);
$post = array('file_contents'=> $cFile);
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: multipart/form-data'));
curl_setopt($ch, CURLOPT_URL,'http://www.url.to/remote/server');
curl_setopt($ch, CURLOPT_POST,1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
$result=curl_exec ($ch);
curl_close ($ch);
Lucarnosky
  • 514
  • 4
  • 18

1 Answers1

0

Sorry for the time, but i think the best solution is to use tmpfile() :

$temp = tmpfile();
fwrite($temp, $result);
fseek($temp, 0);
fclose($temp);

It's work with PHPSpreadSheet Library. Solution for the next ones arriving here, because it was in top position of a PHPSpreadSheet search.

Thanks.

Sigri44
  • 187
  • 2
  • 13
  • 1
    Can you explain that further? What does the code do to solve that mysterious problem? – Nico Haase May 15 '19 at 08:50
  • It avoids the need to save the file and then open it with Excel. The code is used to generate a temporary file in order to be able to parse it, and delete the file at the end. – Sigri44 May 17 '19 at 14:55
  • Please add all such information to the answer itself, not to the comment section – Nico Haase May 17 '19 at 15:35