PHP Generate file and promt save as

孤街浪徒 提交于 2021-02-05 08:57:05

问题


I have a script for generate a .csv file and then download, but I need the dialog "Save as" for the user because I want a fast replace of the old file.

An easy example, I download the file "myFile.csv", then edit the data and download again for the refresh, but I need to REPLACE the file, and the browser download it as "myFile (1).csv", so I need to change the name. The point here, time is crucial.

I want the dialog "Save as" for the force as the same name and replace it.

A MWE:

<?php
header('Content-Type: text/csv;charset=Windows-1252');
header('Content-Disposition: inline; filename="myFile.csv";');

$delimiter = ',';

$f = fopen('php://output', 'w');

foreach ($cars as $car) {
    fputcsv($f, [$car['id'],$car['model'],$car['color'],null], $delimiter);
}
fclose($f);
?>

EDIT: (Aclaration)

Tested on Google Chrome 68

I just realized that I did not mention an important point, when selecting the link for the download, the download automatically starts as myFile (1).csv and that's what I want to stop, I do not want the automatic download, I want the save dialog, save or cancel.


回答1:


An author can suggest a filename for the file to be saved as (via the URL, the download attribute, or the Content-Disposition response header), but there is no way to make the browser ask the user what filename they want to use.

It is entirely up to the browser if it saves to a default directory or prompts the user for a location to save to.

What you want is impossible.




回答2:


and the browser download it as "myFile (1).csv"

Because it realizes that there already is a file called myFile.csv in the target folder.

I want the dialog "Save as" for the force as the same name and replace it.

That is not possible.

You can only specify a file name to be suggested to the user when downloading the file, but you can not force anything in this regard.

If the browser is set to automatically save such downloads into a directory without any further user interaction, then there is nothing that can be done about this at all (besides the user changing their settings); If the user has to confirm each download, then they will have to correct the automatically suggested file name myFile (1).csv to just myFile.csv manually before they safe the file, so that they will then get prompted to confirm whether they want to replace the existing file.




回答3:


Try using this header instead:
Content-Disposition: attachment; filename="myFile.csv"

From: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Disposition

Content Disposition
The first parameter in the HTTP context is either inline (default value, indicating it can be displayed inside the Web page, or as the Web page) or attachment (indicating it should be downloaded; most browsers presenting a 'Save as' dialog, prefilled with the value of the filename parameters if present).

Content-Disposition: inline Content-Disposition: attachment Content-Disposition: attachment; filename="filename.jpg"



来源:https://stackoverflow.com/questions/52052089/php-generate-file-and-promt-save-as

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!