skip to Main Content

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 = PhpOfficePhpSpreadsheetIOFactory::load('template.xlsx');
$worksheet = $spreadsheet->getActiveSheet();
$filename = 'write.xls';
$worksheet->getCell('A1')->setValue('John');
$worksheet->getCell('A2')->setValue('Smith');
$writer = PhpOfficePhpSpreadsheetIOFactory::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

2

Answers


  1. 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>";
    
    Login or Signup to reply.
  2. I think it is the load() usage issue, your code works with following correction in my site :

    $file_loc = 'template.xlsx';
    $file_type = PhpOfficePhpSpreadsheetIOFactory::identify($file_loc);
    $reader = PhpOfficePhpSpreadsheetIOFactory::createReader($file_type);
    // $spreadsheet = PhpOfficePhpSpreadsheetIOFactory::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 = PhpOfficePhpSpreadsheetIOFactory::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();
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search