using PHP to remove the extension from a file and then downloading it

前端 未结 3 1658
我在风中等你
我在风中等你 2021-01-29 14:28

I recently had a asked a question very similar to this one, however after evaluating that I did not explain it in the best way I have come back once again explaining it in a gre

3条回答
  •  死守一世寂寞
    2021-01-29 14:42

    Just use headers to specify response type.

    $filepath = '/wherever/the/file/is.png';
    $filename = 'new-cool-name';
    header('Content-Type: whatever/content-type-is');
    header("Content-disposition: attachment;filename=$filename");
    readfile($filepath);
    

    This basically sends a response with specified content-type as an attachment and the body of the attachment contains the file contents. If you never sure what's the content type is, then just use application/octet-stream

提交回复
热议问题