PHP“另存为...”文件

I want to allow users to download a specific file from the server and let them choose the name of the file.

It is a JSON file.

I have the following PHP script that works but it is naming the file automatically:

if (file_exists($myFile)){
    header ("Content-Type: application/download");
    header ("Content-Disposition: attachment; filename=$myFile");
    header("Content-Length: " . filesize("$myFile"));
    $fp = fopen("$myFile", "r");
    fpassthru($fp);
} else {
    echo "no file exists";
};  

Simply change header ("Content-Disposition: attachment; filename=$myFile"); to be header ("Content-Disposition: attachment; filename=$chosenFileName"); where $chosenFileName is the name provided by the user.

Browsers open that save as dialog depending on the content-type. Within the browser a user can specify which mime-type should open in which way.

btw. normally you specify for json the mime type application/json - see RFC 4627

you could try to set the type to application/octet-stream.

but as i wrote - it depends on user settings.

In firefox it can be modified this way: https://support.mozilla.org/en-US/kb/change-firefox-behavior-when-open-file

Step - 1 . First when click on download button open a form with input filename (open popup or another page its upon you)

Step - 2 Now give the name of file (I mean enter name in text box and click on submit);

Step - 3 On submit send request as a post (method = 'POST') to the download file. this look like $_POST['filename'];

Step - 4

if($_POST['filename']){
    $filename = $_POST['filename']; 
}

if (file_exists($myFile)){
    header ("Content-Type: application/download");
    header ("Content-Disposition: attachment; filename=$filename ");
    header("Content-Length: " . filesize("$filename "));
    $fp = fopen("$filename ", "r");
    fpassthru($fp);
} else {
    echo "no file exists";
};  

I hope this will help you ..!!