1

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.

3
  • You have marked the first paragraph of your question as a quote. Who are you quoting? If you are not quoting anyone, please edit the question so it isn't marked as a quote. Commented Aug 28, 2018 at 7:47
  • i think the solution would be to simply append the file name with some sort of alphanumeric short token, kinda like - myfile_234wdfdf89sd myfile_adf09dfjddf Commented Aug 28, 2018 at 7:59
  • because there's no such Content-Disposition to instruct the browser and javascript can't do that... Commented Aug 28, 2018 at 8:03

3 Answers 3

2

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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.

2 Comments

Obviously, what I'm looking for is that the user can choose the name, because until now he can not do it.
Well then this is not so much a programming question, but one of how to configure your browser. You need to set it to ask you where to save downloads each time, and not just automatically store them in a pre-defined folder. (Chrome however seems to ignore that setting lately; I’ve heard multiple reports that it seems to at least partially ignore the “Ask where to save each file before downloading” setting sometimes, and stores the download into the configured folder nevertheless.)
-1

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"

1 Comment

"most browsers presenting a 'Save as' dialog" —That is no longer true. For many years, most browsers have just downloaded the file to a default downloads directory as the standard behaviour.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.