'PHP Spreadsheet saving document in root folder

Currently I'm using PHPOffice library for exporting my data to a excel spreadsheet. My current code is correctly giving all the table data in a spreadsheet format but it does not get downloaded in the users machine for some reason. Whenever I click the export button, it gets saved to the root folder of my directory C:\Xammp\htdocs.

Heres my controller code:

    function listing_export_allxls()
    {
        $spreadsheet = new Spreadsheet();
        $sheet = $spreadsheet->getActiveSheet();
        $arrlistings = $this->input->post('listing');
        $lists = str_replace('-', ',', $arrlistings);
        $arrLists = $this->listings_model->exportallxls($lists = '');
        $sheet->setCellValue('A1', 'Listing Sales');
        $sheet->getStyle("A1")->getFont()->setSize(16);
        $i = 0;
        $row = 3;
        $Column = array('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M');
        // Header
        $column_title = array('Ref No', 'Name', 'For', 'Unit No.', 'Unit Type', 'Development', 'Beds', 'Size', 'Price', 'Property', 'Community', 'Agent', 'Listed On');
        for ($i = 0; $i <= count($column_title) - 1; $i++) {
            $index = $Column[$i] . $row;
            $sheet->setCellValue($index, $column_title[$i]);
        }
        // Rows
        for ($j = 0; $j < count($arrLists); $j++) {
            $row++;
            $sheet->setCellValue('A' . $row, $arrLists[$j]['refno']);
            $sheet->setCellValue('B' . $row, $arrLists[$j]['proptitle']);
            $sheet->setCellValue('C' . $row, $arrLists[$j]['property_for']);
            $sheet->setCellValue('D' . $row, $arrLists[$j]['unitno']);
            $sheet->setCellValue('E' . $row, $arrLists[$j]['ctitle']);
            $sheet->setCellValue('F' . $row, $arrLists[$j]['dtitle']);
            $sheet->setCellValue('G' . $row, $arrLists[$j]['bedrooms']);
            $sheet->setCellValue('H' . $row, $arrLists[$j]['bua']);
            $sheet->setCellValue('I' . $row, $arrLists[$j]['price']);
            $sheet->setCellValue('J' . $row, $arrLists[$j]['property']);
            $sheet->setCellValue('K' . $row, $arrLists[$j]['community']);
            $sheet->setCellValue('L' . $row, $arrLists[$j]['agent']);
            $sheet->setCellValue('M' . $row, $arrLists[$j]['listed_on']);
        }
        $sheet->getStyle('A3:M3')->getFill()
            ->setFillType(\PhpOffice\PhpSpreadsheet\Style\Fill::FILL_SOLID)
            ->getStartColor()->setARGB('FFA0A0A0');
        $writer = new Xlsx($spreadsheet);
        $writer->save('all_listings_' . date('dmyhis') . '.xls');
    }

How do I get it to save or pop up in the users machine. I've also tried $writer->save('php://output');, but it didn't save my folder at all.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source