This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to force download of a file?
How to download a file that directly opens in the browser by default.
For ex- In older browsers, if I opened a link of a mp3 song, then it were downloaded; but now-a-days, they start playing in the browser itself. One method is to right-click the link and click save link as and then save the song; but many times, the link provided is a mask to some other link which is actually the song. So what to do in this case?
You need to set the content disposition to attachment in the header like this:
header('Content-Disposition: attachment');
Also to Include a file name you can use :
header('Content-Disposition: attachment; filename=abc.mp3');
Or maybe configure apache[if you're on apache] to have this defined for filetypes you want to force download, like
<FilesMatch "\.(mp3|mov|pdf)">
Header set Content-Disposition attachment
</FilesMatch>
Related
This question already has answers here:
How to fix "Headers already sent" error in PHP
(11 answers)
Force-downloading, from php file
(2 answers)
Closed 9 years ago.
I am trying to make a php script when you load on to that page it downloads a video. However since i do not know anything about headers it seems i can't figure it out so please explain why it works and how header works. I am trying to make the browser download video files.Can someone also explaain the headers and what they do please.
Here is my failing code:
<?php
//Outputing video name
$file_name = $_POST['FileToD'];
//outputting video extension e.g video/mp4
$file_ext= $_POST['FileExt'];
//where the file is kept
$file_path = 'mysever.myadress.com/media/movies/' . $file_name;
header('Content-Type:'.$file_ext);
header('Content-Length:' . filesize($file_path));
header('Content-Description: attachment; filename='.$file_name);
readfile($file_path);
?>
If you want to output a video, then don't start by outputting HTML and then switch to video data as part of the same file. (You can't set response headers after you've started outputting data anyway). Remove everything before <?php and after ?>
$file_url should be the path, on the server's file system, to the file you want to make available. It shouldn't be a URL (unless you want a really inefficient approach or need to proxy from a different server), and if it is a URL then it needs to start with the scheme (e.g. http://).
The content-type needs to be the actual content type of the video (e.g. video/mp4), not a file extension (and it doesn't make sense for it to be provided by the user).
You also need to sanitise the user data. At present (if the errors described above were fixed) then anybody could request any file that exists on the server.
This question already has an answer here:
php: force download to hd?
(1 answer)
Closed 9 years ago.
i was trying to download from an android application a php file stored onto my server.
From the application i call a webservices that have to give me in output the file .php that i need.
On the web i found this that talk about which header set to download a file. It talks about
zip
jpg
txt
pdf
Then i was thinking to develop something like:
Application call ws to get a php page
Ws zips the php file that application needs and give me in putput
Application download the zip file and extract it.
This is a good solution but i was trying to find something better.
Another solution that is really like what i want is highlight_file
Just use it like:
echo highlight_file("myphpfile.php");
The problem is that in order to render good the file, the code is divided by many html tags and just "visually" is the same file but the source is really different.
Is there a way to download the file directly? Thanks! :)
The simplest solution might be:
<?php
header('Content-Type: text/plain');
readfile("myphpfile.php");
this will display the file as text in browser you can click 'save as...'
If you need the browser to popup a 'Save as...' dialog by itself - without displaying the file - then you'll need the Content-Disposition header:
header('Content-Type: text/plain');
header('Content-Disposition: attachment; filename="myphpfile.php"');
readfile("myphpfile.php");
This question already has answers here:
A PHP script to let users download a file from my website without revealing the actual file link in my website?
(4 answers)
Closed 9 years ago.
I want to make images shopping site in which I want people buy images then they can download them.
My problem is how to create hidden path to image that people download the image and don't know the real path of the image.
You can call a php file to download the image and not the real image/path.
Like this you can call the real path inside your php file with something like:
$path = "/public_html/yourPath/";
if (! isset($_GET['img'])) {
die("Invalid URL");
}
$imageName = filter_var($_GET['img'], FILTER_SANITIZE_STRING);
$finalPath = $path.$imageName;
header('Content-type: octet/stream');
header('Content-Type: image/jpg');
header("Content-Disposition: attachment; filename=$finalPath;");
readfile($finalPath);
You can read more about it here.
Store the images in an offline location (not www) and retreive them with PHP, so they can access the image for example like this: http://yoursite.com/index.php?file=filename and then PHP will go and return that file from the offline location. You just need to set the correct headers so the content is not treated like a web page but an image instead. Now obviously, such link is still public so you need to add some more information to it to authenticate the downloader.
I have an anchor tag:
Download Me
I would like for the user to click on it and then have a Save-As dialog box appear with a new filename I determine.
I found this(http://www.w3schools.com/php/func_http_header.asp):
header("Content-type:application/pdf");
// It will be called downloaded.pdf
header("Content-Disposition:attachment;filename='downloaded.pdf'");
// The PDF source is in file.pdf
readfile("file.pdf");
I don't understand where to put those headers. At the top of the page? When I try to place those 3 lines directly above my link I receive the following errors:
Warning: Cannot modify header
information - headers already sent by
(output started at
/home5/ideapale/public_html/amatorders_basic/admin/download.php:38)
in
/home5/ideapale/public_html/amatorders_basic/admin/download.php
on line 118
I got the same error for both of the header lines I just added. Right after that, there are thousands lines of ASCII letters. How can I get the Save-As dialog box to appear using either jQuery or PHP(whatever is easier)?
Please be careful when using Stijn Van Bael's code, it opens you up to some serious security exploits.
Try something like:
--- download.php ---
$allowed_files = array('file.pdf', 'otherfile.pdf');
if (isset($_REQUEST['file']) && in_array($_REQUEST['file'], $allowed_files))
{
$filename = $_REQUEST['file'];
header("Content-type:application/pdf");
header("Content-Disposition:attachment;filename='$filename'");
// The PDF source is in file.pdf
readfile($filename);
exit();
}
else
{
// error
}
--- linkpage.php ---
Download PDF
Download PDF
Probabaly a better way to do this is at the web server level (this can go in .htaccess) This will force all PDF's to be treated as a binary file (forcing the browser to download them) in and below the directory you put this in.
<FilesMatch "\.(?i:pdf)$">
Header set Content-Disposition attachment
ForceType application/octet-stream
</FilesMatch>
Create a new page with the headers and the readfile, then make the download link refer to the page, which will return the PDF file.
For example:
Download Me
Source for download.php:
$filename = $_REQUEST['file'];
header("Content-type:application/pdf");
// It will be called downloaded.pdf
header("Content-Disposition:attachment;filename='$filename'");
// The PDF source is in file.pdf
readfile($filename);
Or you could just use the new HTML5 property download in the anchor tag of your html.
The code will look something like
<a download href="path/to/the/download/file"> Clicking on this link will force download the file</a>
This question already has answers here:
How to force file download with PHP
(12 answers)
Closed 2 years ago.
How do I force the browser to download the current page browsed to? A page with the header Content-type: text/plain for example using PHP?
If a user navigates to that page, a download box should appear (the browser download dialog with the usual "Save As".
Straight from http://php.net/header
<?php
// There is contention over if this MIME type is right, but just use it for now.
header('Content-type: text/javascript');
header('Content-Disposition: attachment; filename="file.js"');
readfile('file.js'); // Echo the file
?>
NOTE: this must be done before any other output (and can be about the only thing on the page, unless you want other output in your file).