Code snippets for symfony 1.x

Navigation

Refine Tags

Snippets tagged "export"

How to send/download a temporary file and delete that file after download complete

The magic method to delete the file at the end of the script is "register_shutdown_function('unlink', $file)".

public function executeExport(){
    $somecontent = "Some content\n";
    //create temporary file
    $file = tempnam(sfConfig::get(''), 'FOO');
 
    // Write $somecontent to our temporary file
    fwrite($handle, $somecontent);
 
    //send file as attachement 
    $this->getResponse()->setHttpHeader("Content-type", "application/octet-stream");
    $this->getResponse()->setHttpHeader("Content-Disposition", "attachment; filename=\"export.csv\"");
    $this->getResponse()->setContent(file_get_contents($file));
 
    //delete temporary file
    register_shutdown_function('unlink', $file);
 
    return sfView::NONE;
}
 
by Cristian Baciu on 2009-02-23, tagged delete  download  export  file  temporary 
(1 comment)