PHP Pass File Handle to user so that file downloads & saves to their machine

前端 未结 2 1887
傲寒
傲寒 2021-01-17 05:15

I am downloading a file from another server. I wish to push this file to my users rather than saving it to my server.

In other words, pass them the file handle so i

相关标签:
2条回答
  • 2021-01-17 05:42

    You can try this

    $filetype = mime_content_type($filename);
    header('Content-type: '.$filetype);
    header('Content-Disposition: attachment; filename="'.$filename.'"');
    

    UPDATE for your EDIT:

    Do you have errors disabled, since this sounds like the headers already sent error?

    error_reporting(E_ALL | E_STRICT);
    
    0 讨论(0)
  • 2021-01-17 05:49

    You don't have to use fopen() when using readfile();

    Just include the filename inside readfile() like this:

    readfile($_GET['fileUrl']);
    

    Although this is very dangerous security-wise as the user could specify any file on your file server. If you only have a few files you want someone to be able to download perhaps you should store them in an array (or database, preferebly)

    Here's an array example:

    $files = array('file1.jpg', 'file2.png', 'file3.pdf');
    //assume $_GET['file_id'] == 0, 1 or 2
    
    if (file_exists($files[$_GET['file_id']]))
        readfile($files[$_GET['file_id']]);
    
    0 讨论(0)
提交回复
热议问题