try to download file and getting invalid file in response in core php

后端 未结 3 1793
孤独总比滥情好
孤独总比滥情好 2021-01-28 09:00

I download a file but it gives invalid file in return.

Here\'s my download_content.php



        
3条回答
  •  北恋
    北恋 (楼主)
    2021-01-28 09:33

    Your $filename variable contains whole path as below

     header("Content-Disposition: attachment; filename=$filename");
    

    Do like this

    $newfilename = explode("/",$filename);
    $newfilename = $newfilename[count($newfilename)-1];
    
    $fsize = filesize($filename);
    
    Then pass new variable into header
    
    header("Content-Disposition: attachment; filename=".$newfilename);
    header("Content-length: $fsize");
    
    //newline added as below
    ob_clean();
    flush();
    readfile($filename);
    

提交回复
热议问题