file corrupt when click download link

后端 未结 3 657
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-15 10:55

I try to use php to force the image jpg file download, I have implemented eth following code:

html



        
相关标签:
3条回答
  • 2021-01-15 11:29

    Try to use:

    header("Content-Disposition: attachment; filename=\"".basename($fullPath)."\";" );
    
    0 讨论(0)
  • 2021-01-15 11:31

    You need some code that actually sends over the file. See e.g. readfile or fpassthru

    0 讨论(0)
  • 2021-01-15 11:46

    Here you are example how to use readfile function

    <?php
    $file = 'monkey.gif';
    
    if (file_exists($file)) {
        header('Content-Description: File Transfer');
        header('Content-Type: application/octet-stream');
        header('Content-Disposition: attachment; filename='.basename($file));
        header('Content-Transfer-Encoding: binary');
        header('Expires: 0');
        header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
        header('Pragma: public');
        header('Content-Length: ' . filesize($file));
        ob_clean();
        flush();
        readfile($file);
        exit;
    }
    ?>
    
    0 讨论(0)
提交回复
热议问题