PHP Generate file and promt save as - php

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.

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.

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.

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"

Related

i want to change the save as dialog filename when the user right clicks

I am using PHP and javascript/jQuery for my project. I want to change the filename for the save as dialog when the user right clicks on an image and selects save as. (for example i want to name every "save dialog" filename "image.png"). Thanks in advance.
The file name that the browser gives in the save dialog CAN be changed, but not in Javascript. Usually this is done when you don't give a direct link to an image, but use a PHP script such as < img src="image.php?id=18" >
To do this you just need to send proper http headers in image.php, for example:
header('Content-type: image/jpg');
header('Content-Disposition: inline; filename="' . $filename . '"');
If you use non-ASCII file name, you will need to encode it and unfortunately the encoding is different for different browsers:
For IE use rawurlencode($filename)
For FF use base64 with charset specification, such as '=?UTF-8?B?'.base64_encode($filename).'?='
If you specify "attachment" instead of "inline" in Content-Disposition header, the browser will not try to display the image but will immediately prompt user to download it.

html/php user download files

On my web server, I have a bat (harmless) file.
And I have code,
Test Bat File
But when the user clicks, it shows the code instead of downloading the file.
You have to right click "save as.." to download the bat file.
Is there way that when a user clicks, it downloads (not having to right click save as)?
Maybe a pop up window that asks user if he/she wants to download the file or not?
you could write a php file, which adds a content-disposition header, sets the mime type to something binary and echo the files content.
Example:
file.php
$batchfile = file_get_contents('batchlocation');
$size = strlen($batchfile);
header('Content-Disposition: attachment; filename="downloaded.bat"');
header('Content-Type: BAT MIME TYPE or something like application/octet-stream');
header('Content-Lenght: '.$size);
echo $batchfile;
You can do so through setting the file in the PHP header() function.
It is explained here:
How to Automatically Start a Download in PHP?

File open/save as dialog and mime types

I am working on a simple document management system for a site - the user can upload around 20 different file types and the docs are renamed then stored in a folder above www, an entry is created in a docs table to capture meta data entered by the user and the item is then retrieved via another php file so the stored location for the files are hidden from the user.
When a user clicks to download a file using a simple a href it calls, for example, "view.php?doc=image.jpg" - when they do this currently the file opens in the browser so a jpg opens a window with pages of "wingdings" like characters etc.
I would like to be able to force a open/save dialogue box so the user decides what to do and my app doesn't try to open in the browser window with the above results.
From a previous posting I found I know I cannot pass the mime type in the "a href" tag so what other options do I have? Could I put header information into the below view.php file, for example?
$_file = $_GET['doc'];
$filename = './dir/'.$_file;
if (file_exists($filename)) {
echo file_get_contents('./dir/'.$_file);
} else {
echo "The file $_file does not exist";
}
;
You could use get_headers() to get the MIME type header of the desired file, and then use header() to output those headers into the file you're showing.
Alternatively, to simply force downloads, this:
header('Content-Description: File Transfer');
header("Content-type: application/octet-stream");
Should do it.

File name changed on download

i want to implement code so that when user will download that file, name of the file should be changed
as example
$uploaddir="files/userid/";
$filename=rand(1000,9999).time().rand(1000,9999);
move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile);
suppose using this code file is uploaded it will be stored on server as name like this
23451232325654.pdf
but for user he/she will have logical name for it Like learn_php.php
when user want to download this file he/she will have this link to download
www.example.com/files/userid/23451232325654.pdf
but this file not stored on user's pc when downloaded as 23451232325654.pdf but i want to store it as their logical name as shown above
learn.php
You can do this no problem. You just need a download script that will first send the correct header. In this case, the header should be something like:
header('Content-Disposition: attachment; filename="learn_php.pdf"');
See example 1 in the php docs.
So instead of linking directly to the file (for example: http://website.com/content/129312.pdf), you would link to your download script (for example: http://website.com/download.php?file=129312.pdf).
And download would first send the headers, then the file contents.
Obligatory note about security: Using the filename directly from $_GET without sanitizing it opens up a huge security issue. If you do it this way you NEED to sanitize it.

php save recordset to local file

I have a list of data compiled from a mysql recordset when I click a button on one of my pages. The data is stored in a variable $list.
It's a site activity log, and the button is a backup button.
Is there any way that I could make it open a SAVE AS dialogue box so I can save that data to a text file on my local comp?
when you click your "back up" button, you should get the user to a new script: this script should take the $list variable from the DB again and format it into a text file, then in order to make it available to the user's browser as a downloadable file, you should use headers (look at http://php.net/manual/en/function.header.php) like this:
<?php
// We'll be outputting a PDF
header('Content-type: application/pdf');
// It will be called downloaded.pdf
header('Content-Disposition: attachment; filename="downloaded.pdf"');
// The PDF source is in original.pdf
readfile('original.pdf');
?>
In this case this is a pdf file (example is from the above link). Changing the content-type to the proper mime-type ("Content-Type: text/plain" for example) and setting the right file name, all that you echo will be sent to the browser as an attached file.
If any question, ask :)
after generate that file just set the physical path of that file and throw header so it will be download at your local system
Cheers

Categories