I'm using PHPExcel to generate a spreadsheet to download.

<?php
    $objPHPExcel = new PHPExcel();
    $emailReport = 'tmp/company.xls';
    // Remove previous file if it exists.
    $testdel=unlink($emailReport);
    if(!$testdel){
        echo "Unable to remove previous Spreadsheet<hr/>";
    }
    echo '<a href="'.$emailReport.'" class="dbutton">Download Spreadsheet</a><hr/>';
    $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(1, 1,'Old Value');
    $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
    $objWriter->save($emailReport);
    ?>

All well and good.

The spreadsheet is created and it holds the text in cell A1.

But if I change the php file, perhaps by changing $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(1, 1,'Old Value');to $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(1, 1,'New, Improved Value'); I'm expecting the new spreadsheet to contain "New, Improved Value" in cell A1, but when I click the link to download it, the version of the spreadsheet that is downloaded contains the old value.

Yet when I download the file using FTP, the file has indeed been updated.

Presumably the browser holds the file in cache?

How do I make sure that the correct version is downloaded?

You can invalidate your cache by using header(). Example:

header('Cache-Control: private, max-age=0, no-cache');

on the first line of your file. But I don't think that'll work in your example. It only works on the page that offers the file as a download. That means that in your case, you'd have to make a page that gets the contents of your xls file and outputs them directly to the browser with the correct headers.

Have you tried clearing your browser's cache (ctrl + shift + del) and downloading the file again? Or starting your browser in p0rn cough private mode and downloading the file again?

Thanks for the suggestions.

I've decided to add the time onto the filename, so that the browser won't refer to the cached version.

It was difficult to work out what was going on and why the changes in the spreadsheet weren't being rendered.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.