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

前端 未结 3 1624
我在风中等你
我在风中等你 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

    0 讨论(0)
  • 2021-01-29 15:01

    Usually when you set out to push a file for downloading from a serverside script, you do so by utilizing http headers like https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Disposition The filename of the downloadable file is specified in that header

    0 讨论(0)
  • 2021-01-29 15:01

    Okay so to remove an extention from a file you could do is

    $withoutExtion = preg_replace('/\\.[^.\\s]{3,4}$/', '', $youfilename);

    ...followed by your file download code

    0 讨论(0)
提交回复
热议问题