문제

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)

도움이 되었습니까?

해결책

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

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top