prompt save file download box on click for multiple files - php

php
<a href="<?php echo $path_data['wall_path'];?>">
<?php echo $dimensions_data['width'] . "x" . $dimensions_data['height']; ?></a>
which outputs
<li>640x960</li>
<li>640x1136</li>
<li>720x1280</li>
<li>768x1280</li>
<li>1080x1920</li>
Now what I need to do is when the user clicks any one of the sizes, I want to prompt a save file dialog box containing the appropriate size file. I've read Forcing to download a file using PHP and PHP Force File Download, but based on my understanding, these are for single file downloads. Please correct me if not.
Restrictions:
don't want to forward to another page like download.php file when user clicks a link.

Normally a browser would not show a download dialog for an image, it will display the image instead, what is the desired behaviour. You can then click "Save As ..." and save the image.
What the browser does with a files depends on the mime type which the web server sends for that file. For jpg images it would be
image/jpeg
If you really want to trick the browser to show a save dialog you need to send a different mime type for that files, like:
application/octet-stream
This can be done using a download.php file (or whatever) which modifies the header and outputs the file. The existence of such a download.php can be hided from the user using rewritten urls.
If you don't want that, you need to tell the web server that it should send a different mime type for that files. If you run apache for example, you can add the following line to your .htaccess file:
AddType application/octet-stream .jpg

Related

PHP Generate file and promt save as

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"

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.

Serving images and saving cookies on the user's machine

I know it's possible to plant a cookie on a user's machine when he is loading an image from my server, by altering the apache settings.
However, I want to know if it's possible to include an image in HTML code that will have parameters in it and I can activate some script to log these parameters, for example:
<img src="http://www.mysite.com/myimage.jpg?somcode=123&customer=abcd" />
In return to loading the picture, I would like to save a cookie on the user's machine with somecode=123 and customer=abcd and also to save this info in my db.
Is this possible?
Thanks,
Instead of using .jpg for the filename, just use .php. Then you don't have to mess around with server configurations and normal image files still get served correctly:
<img src="http://www.mysite.com/myimage.php?somcode=123&customer=abcd" />
Then your myimage.php file:
<?php
// Your tracking and processing code here
$customer = $_GET['customer'];
// Then, either:
header("Location: /urlpath/to/the/actual/image.jpg"); /* Option 1 */
// OR:
header("Content-Type: image/jpeg");
readfile("/filepath/to/the/actual/image.jpg"); /* Option 2 */
I prefer Option 1 because it allows your web server to very efficiently serve the actual image instead of PHP (Option 2).
I'm pretty sure you could do that, Just make apache treat the .jpg extension as php files.
You could add the following to your .htaccess file in the specific folder (don't put it in the root or it will treat all JPGs this way);
AddType application/x-httpd-php .php .jpg

How do I load an image in PHP

I want code that loads an image to a PHP server and then send it to browser.
For example I want sample.php to send an image to browser once it is requested.
in other words, I want to create a PHP file that acts like a proxy for an image.
why are you doing this?
why don't deliver the image directly?
if you are trying to display a random image you may as well just redirect to the image using
header("Location: address-of-image");
for delivering the file to your clients from your server and not from its original location you can just do. however your php.ini settings need to allow external file opens
readfile("http://www.example.com/image.jpg")
correct headers are not required if you are going to display the image in an img tag,
altough i would recommend it. you should check the filetype of the image or in most cases just set an octet-stream header so the browser doesnt assume an incorrect type like text or something and tries to display binary data.
to do so just do
header("Content-type: application/octet-stream")
one more thing to consider may be setting correct headers for caching...
You need to use
$image = fopen("image.png");
Modify the headers(not sure exacly if it's correct)
headers("Content-type: image/png");
And then send the image
echo fread($image, file_size("image.png"));

Categories