Pergunta

I have the following function to force download a file:

static public function download($file, $options=array()) {
    $content = (isset($options['content'])) ? $options['content'] : '';
    $contentType = (isset($options['contentType'])) ? $options['contentType'] : '';
    header('Cache-Control: public');
    header('Content-Description: File Transfer');
    header('Content-Disposition: attachment; filename='.File::filename($file));
    header('Content-Type: '.$contentType);
    header('Content-Transfer-Encoding: binary');
    if ($content!='') {
        echo $content;
    } else {
        readfile($file);
    }
}

i send a PDF file and contentType = "application/pdf". the problem is that when i try to open the downloaded PDF file it says "There was an error opening this document. The file may be corrupt". Is weird because i can open the original file and they look exactly the same (filename, size, etc)

Foi útil?

Solução

Ensure there is no output before this function is ran, and for good measure, use the exit construct at the end of this function :)

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top