I am dynamically generating image in php. The image has a fixed name. I wants a button or hyperlink and onclick of that button, users should be able to export image rather than right click and save as image options. The problem is that in case of excel,pdf or doc files, I can specify the path of file and browser automatically asks for the open or save option but for images, it opens them in separate window.I want same dialog box for saving the image as for the other files like excel,pdf.Please help me on this.
Thanks
you can set the header to force download (for PHP):
header("Content-Description: File Transfer");
header("Content-Disposition: attachment; filename=$file");
header("Content-Type: image/png");
Then you can read the file
readfile($file);
What you can do is force image download using .htaccess if you are using Apache or directly via php using header tags
PHP Example
$file = "PATH TO FILE" ;
$fileName = basename($file);
$fileSize = filesize($file);
set_time_limit(0);
header("Pragma: public");
header("Expires: 0"); // Cache Options
header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); // Cache Options
header("Content-type: application/octet-stream");
header("Content-Disposition: filename=\" " . $fileName ."\"");
header("Content-length: $fileSize");
readfile($file);
Related
I have a zip files that I want users to be able to download. The trick is I don't want the users to see what the url is and I don't want to download the file to my server.
So I want users to click a link like this:
http://example.com/download/4
which server-side accesses my S3 bucket with this url:
https://s3.amazonaws.com/my-bucket/uploads/4.zip
I've tried cURL, using S3 methods, and various headers() in my download($file_id) function but can't get this to work. This has to be easy, right?
Your right, its quite easy. Probably you will have to write something like this:
$path = '/my-bucket/uploads/4.zip'; // the file made available for download via this PHP file
$mm_type="application/x-compressed"; // modify accordingly to the file type of $path, but in most cases no need to do so
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: public");
header("Content-Description: File Transfer");
header("Content-Type: " . $mm_type);
header("Content-Length: " .(string)(filesize($path)) );
header('Content-Disposition: attachment; filename="'.basename($path).'"');
header("Content-Transfer-Encoding: binary\n");
readfile($path); // outputs the content of the file
exit();
You set various headers to make your user download the .zip. Afterwards you put your file into the output buffer with readfile() Afterwards you end your script with exit() for security's sake. This should work for you! Remember to change the path to your file.
Thanks #Xatenev for the help. This is actually what worked for me:
$path = '/my-bucket/uploads/4.zip'; // the file made available for download via this PHP file
$mm_type="application/zip"; // modify accordingly to the file type of $path, but in most cases no need to do so
header("Content-Type: " . $mm_type);
header('Content-Disposition: attachment; filename="'.basename($path).'"');
readfile($path); // outputs the content of the file
exit();
<?php
$file = $_GET['name'];
$path = './curr/'.$file.'.pdf'; // the file made available for download via this PHP file
$mm_type="application/pdf"; // modify accordingly to the file type of $path, but in most cases no need to do so
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: public");
header("Content-Description: File Transfer");
header("Content-Type: " . $mm_type);
header("Content-Length: " .(string)(filesize($path)) );
header('Content-Disposition: attachment; filename="'.basename($path).'"');
header("Content-Transfer-Encoding: binary\n");
readfile($path); // outputs the content of the file
?>
This is a snippet of code in file.php. I am referring to the file using:
File 1
The intent is that on click of the link, ./curr/First File.pdf should download. I do get a download, but on inspecting, it's the webpage with the pdf embedded in the file. Could anyone assist?
If you want to have just the PDF loaded, the above code is all code to be executed.
Drop all surrounding menus, header or footers. Make sure, that no HTML or any other output besides the PDF from readfile() remains, when calling this link.
Try to change the content type to :
header("Content-Type: application/force-download");
I created a form, which, when a contained button is clicked, should open a download dialog to download a certain file. The file is placed on the same server.
I tried:
header("Content-Type: application/force-download");
header("Content-Disposition: attachment; filename=" . $file . '"');
Where $file is a local path + the file name, for example c:\mypath\myfile.xls. This does not work though. It offers me a file, but its not a valid file. How else could I do that?
Note: I wrote c:\ because its still on my local machine for testing.
Thanks!
Try this
header("Pragma: public", true);
header("Expires: 0"); // set expiration time
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Content-Type: application/force-download");
header("Content-Type: application/octet-stream");
header("Content-Type: application/download");
header("Content-Disposition: attachment; filename=".basename($file));
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".filesize($file));
die(file_get_contents($file));
I think file_get_contents() function is no longer work with PHP 5.0.3
Try this :
$path = "http://www.example.com/files/";
$filename = "abc.gif";
header("Content-Type:image/gif");
header("Content-Disposition: attachment; filename=".$filename);
header("Cache-control: private");
header('X-Sendfile: '.$path);
readfile($path);
exit;
PHP runs in server side, you can not download the files in clients machine.
Upload the files to server and then give that path for download.
Path must be refered from the site root...move the file
ex:
script path : C:/wamp/www/test.php
file C:/script.js
then:
if(file_exists('../../user.js'))
{
echo "OK";
}
Still a bad ideea..
can anyone know on how could I force to download a file without displaying DialogBox (Open/Save). The below code was is my test script to download created excel file but the dialog box appears to download a file.
$filename ="excelreport.xls";
$contents = "testdata1 \t testdata2 \t testdata3 \t \n";
header('Content-type: application/ms-excel');
header('Content-Disposition: attachment; filename='.$filename);
echo $contents;
I want to automatically download file and save it on specified directory without dialog box
You can force to download instead of showing it but I think it is not possible to force the browser in general to download it without a prompt
// fix for IE catching or PHP bug issue
header("Pragma: public");
header("Expires: 0"); // set expiration time
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
// browser must download file from server instead of cache
// force download dialog
header("Content-Type: application/force-download");
header("Content-Type: application/octet-stream");
header("Content-Type: application/download");
Source: php.net
You cannot do this for security reasons.
The builtin browser of my ebook-reader (Sony PRS-T1) somehow doesn't like to download .epub files.
Normally it opens .epub files as if they were text-files.
With this php-download-script I managed to force the browser to download files I store on my server:
<?php
$path = $_GET['path'];
$mimeType = $_GET['mimeType'];
if(!file_exists($path)) {
// File doesn't exist, output error
die('file not found');
} else {
$size = filesize($path);
$file = basename($path);
// Set headers
header("Pragma: public"); // required
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: private",false); // required for certain browsers
header("Content-Description: File Transfer");
header("Content-Disposition: attachment; filename=\"$file\"");
header("Content-Type: $mimeType");
header("Content-Transfer-Encoding: binary");
header("Content-Length: $size");
// Read the file from disk
readfile($path);
}
exit();
?>
Now, the PRS-T1 would download the file but for some reason I don't understand it will change the file extension from .epub to .htm - this is weird.
But it seems like there is a way to do it right: when I download a .epub file from readbeam.com it works just like expected (I found this hint at http://www.mobileread.com/forums/showthread.php?t=163466).
What is it, that makes the difference betweeen their configuration and mine?
Here's what I found out using firebug:
http://tinypic.com/r/vzzkzp/5
http://tinypic.com/r/2h7pbth/5
Your Content-Type header doesn't match the one from readbeam.
application/epub zip != application/epub+zip
The + is probably being seen by PHP as a space since it seems you are passing it via $_GET.