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

前端 未结 2 1886
傲寒
傲寒 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: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']]);
    

提交回复
热议问题