Walid Tunidir
Walid Tunidir

Reputation: 3

get empty file after download

i want to download a file of projet but i get it empty. i'am using a spreadsheet librairy Notice : i a make a dump after save function , my file is full and not empty in the path directory of project Someone can help me !

bellow is my code :

$spreadsheet = \PhpOffice\PhpSpreadsheet\IOFactory::load('template.xlsx');
$worksheet = $spreadsheet->getActiveSheet();
$filename = 'write.xls';
$worksheet->getCell('A1')->setValue('John');
$worksheet->getCell('A2')->setValue('Smith');
$writer = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($spreadsheet, 'Xls');
$writer->save($filename);  die;
// to download file
header('Content-Type: application/vnd.ms-excel');
header("Content-Length:".filesize($filename));
header("Content-Disposition: attachment;filename=$filename");
header('Cache-Control: max-age=0');
$writer->save('php://output'); 
exit();

i except a full file after downloading it

Upvotes: 0

Views: 597

Answers (2)

Zeikman
Zeikman

Reputation: 747

I think it is the load() usage issue, your code works with following correction in my site :

$file_loc = 'template.xlsx';
$file_type = \PhpOffice\PhpSpreadsheet\IOFactory::identify($file_loc);
$reader = \PhpOffice\PhpSpreadsheet\IOFactory::createReader($file_type);
// $spreadsheet = \PhpOffice\PhpSpreadsheet\IOFactory::load('template.xlsx');
$spreadsheet = $reader->load($file_loc);

$worksheet = $spreadsheet->getActiveSheet();
$filename = 'write.xls';
$worksheet->getCell('A1')->setValue('John');
$worksheet->getCell('A2')->setValue('Smith');
$writer = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($spreadsheet, 'Xls');

// save a physical file in server, you can skip this actually
$writer->save($target_dir . $filename);
// die; // don't die, be happy (^_^)

// to download file
header('Content-Type: application/vnd.ms-excel');
header("Content-Length:" . filesize($filename));
header("Content-Disposition: attachment;filename=$filename");
header('Cache-Control: max-age=0');
$writer->save('php://output');
exit();

Upvotes: 0

Joseph
Joseph

Reputation: 28

This function would work:

define ("ONE_DAY", 86400);
function getExisting()
{
    $rootFolder = "pathTodirectory";
    //first clear old files
    $files = scandir($rootFolder,1);
    array_pop($files); array_pop($files);
    foreach($files as $file)
    {
        $fp = $rootFolder . DIRECTORY_SEPARATOR . $file;
        $filemtime=filemtime($fp);
        if (time() - $filemtime >= (2 * ONE_DAY))unlink($fp);
    }//end clearing old files
    //second rescan folder for current files
    $files = scandir($rootFolder,1);
    array_pop($files); array_pop($files);
    $existing = array_reverse($files);
    return $existing;                
}

$existing = getExisting();
echo "\n<p> Select file or enter office number to review inventory:"; 
echo "\n    <ul>";
foreach($existing as $rpt)
{
    $spd =  "pathTodirectory" . $rpt;  \\make sure to follow up with relative path name here also
    echo "\n           <li><a href=\"$spd\" >" . $rpt ."</a></li>";
}
echo "\n    </ul>";

Upvotes: 1

Related Questions