PHP/ HTML: Download PDF link working on page load - javascript

I'm using following code
<?php
$file = 'COMPANY_PROFILE.pdf';
if (! file) {
die('file not found'); //Or do something
} else {
// Set headers
header("Cache-Control: public");
header("Content-Description: File Transfer");
header("Content-Disposition: attachment; filename=$file");
header("Content-Type: application/zip");
header("Content-Transfer-Encoding: binary");
// Read the file from disk
readfile($file);
}
This automatically gives a download file prompt and if i use something like this:
Download PDF
it opens the PDF in the browser, so how do I fix this issue in a way that file gets downloaded on button click?

You can achieve your desired results by different methods. In this way what you are doing right know you are telling php to look for a file if it doesn't exist just show that file doesn't exit other wise show document that exist.
But what you are looking for should perform on some kind of an event. e.g Button Click.
You can achieve this by these kind of method.
AJAX Call
By an ajax call on click on a button to go to this php function.
Posting PHP
Either you can post some thing to PHP and in your PHP code you can tell your code if this specific name has been posted then download pdf otherwise not to.

Related

PHP - How to send textarea value with JavaScript AJAX and output it as .txt file for browser to download?

I have a textarea, text input and a button wich sends textarea and text input values (using AJAX post method) to PHP site. Now, I want PHP to output textarea value as .txt file so that it can be download via browser.
My PHP code looks like this:
$text=trim($_POST['text']); // textarea value
$fileName=$_POST['fileName'].".txt"; // text input value
header("Content-Type: text/plain");
header('Content-Disposition: attachment; filename="'.$fileName.'"');
header("Content-Length: ".mb_strlen($text));
print($text);
Nothing happens, so can you help me please. Thanks. :)
Have you considered not sending the text to the server and just creating a download client-side?
This post has some useful suggestions you could try to create a download by bypassing the server completely Create a file in memory for user to download, not through server
Maybe you should clean the buffer
while (#ob_end_clean());
ob_start();
header("Content-Type: text/plain");
header('Content-Disposition: attachment; filename="'.$fileName.'"');
header("Content-Length: ".mb_strlen($text));
print($text);
ob_end_flush();
exit();

pdf download using php and onclick function

I have the following code.
<div onClick="registerationform.pdf">something</div>
<?php
header('Content-disposition: attachment; filename=registerationform.pdf');
header('Content-type: application/pdf');
readfile('registerationform.pdf');
This code directly downloads the output if the page is loaded. But I need the pdf to get downloaded only if the something button is clicked.Help me
Php code is executed before any page content is shown or any javascript is executed, and not exactly sequentially as you see it in your example.
What you want is probably to create another php page downloadpdf.php which includes those headers you specified, and redirect the user to that page through a link:
link.php:
Download PDF
Note: target="_blank" is added here so the actual page is not redirected but instead the new page is opened in a new tab-> the browser downloads the file and immediately closes the tab, "feeling" like it's an immediate download from the current page you are on.
downloadpdf.php
<?php
header('Content-disposition: attachment; filename=registerationform.pdf');
header('Content-type: application/pdf');
readfile('registerationform.pdf');

Javascript+PHP XMLHttpRequest download prompt

What I am doing is to using PHP to get an external file, and send it to client’s browser. But the following code doesn’t pop up the downloading prompt. Using Chrome's developer tool, I can see in Network -> Response that the data is correctly fetched. But nothing happens to the browser.
<?php
header("Content-Disposition: attachment;filename=Example.zip");
header('Content-Description: File Transfer');
header("Content-Type: application/force-download");
header('Content-Transfer-Encoding: binary');
header("Content-Length: 512");
header("HTTP/1.1 206 Partial Content");
$x=fopen("http://www.example.com/example.zip","r");
echo fread($x,512);
fclose($x);
exit;
?>
UPDATE: In fact, I have figured out where the problem was: I did not invoke the PHP using user's click, but javascript's XMLHttpRequest. When I directly visit my above PHP page, everything works perfectly. Sorry about that. But now the question would be: is it possible to trigger download prompt using javascript XMLHttpRequest?
UPDATE 2: So here is what I wish to accomplish: I have a page with a "download" button, clicking the button will trigger javascript XMLHttpRequest to invoke the PHP page (in this way, the browser address bar will remain the same, i.e. it will not visit the PHP page). I would like to use this background XMLHttpRequest to invoke the PHP page, which returns the content (with all legit headers, i.e. Content-Type, ...) that will invoke the download prompt for the user.
Don’t use this line:
header("Content-Type: application/force-download");
If you are forcing a zip file to download, use the actual MIME type for zip files:
header("Content-Type: application/zip");

How to show the save dialog using HTML

How can I force the save file download dialog box when I click an <a> tag?. There is a PDF file available on a remote server and when a user clicks that link we want to download that PDF file to their local system.
Thanks
Normally when you link a file that file will always display inside of the browser because the browser loads it and automatically determines the content type based on the file extension. So when you click on a link like a jpg image pdf etc the browser knows it's an image/file and will display that file. You can of course always use the browser short cut menu and use the Save Target As option to save the file to disk.
If you want to do this automatically when a link is clicked from the server side, you have to send the file back yourself rather and add a couple of custom headers to the output. The way to do this is to use Response.TransmitFile() to explicitly send the file from your ASP.NET application and then add the Content Type and Content-Disposition headers.
So You neded to use headers liek below:
header('Content-Disposition: attachment; filename="filename.pdf"');
Here is an exapmle might help you :
http://www.west-wind.com/weblog/posts/2007/May/21/Downloading-a-File-with-a-Save-As-Dialog-in-ASPNET
change your header values..
Ex
header('Content-Disposition: attachment; filename="downloaded.pdf"')
path = "path/to/file.pdf";
$filename = "file.pdf";
header('Content-Transfer-Encoding: binary'); // For Gecko browsers mainly
header('Last-Modified: ' . gmdate('D, d M Y H:i:s', filemtime($path)) . ' GMT');
header('Accept-Ranges: bytes'); // For download resume
header('Content-Length: ' . filesize($path)); // File size
header('Content-Encoding: none');
header('Content-Type: application/pdf'); // Change this mime type if the file is not PDF
header('Content-Disposition: attachment; filename=' . $filename); // Make the browser display the Save As dialog
What he is looking for is not a open save dialogue while downloading something from the host.
He is looking to open a file from the client's file system. You must use the input type file, to do so. But you will not get much privileges to manipulate that using javascript.
And there is no way to prompt a window's save dialogue through standard html.
We need ActiveX or Flash to do it.

how to open the save as dialogue box with javascript

how can I programm a button so that when clicked should open the save as dialogue box?
Put simply, how can I save a file on the local machine using javascript or php? I really don't know how to go about it.
Thanks in Advance.
EDIT:
Thanks for the quick response. I have a table that I have displayed on a web page, and a button, so that when that button is clicked, the user can specify the file path, and save that table as a .txt file to the specified destination.
In PHP, you can set the Content-Disposition header to Attachment, which will tell the browser that the content being served is an attached file, which is to be downloaded:
<?php
header('Content-type: application/pdf');
header('Content-Disposition: attachment; filename="downloaded.pdf"');
readfile('original.pdf');
?>
EDIT
To better fill the requirements of your updated question, you want to create a PHP-file that serves the table in the format you want it to be downloaded.
<?php
header('Content-type: text/plain');
header('Content-Disposition: attachment; filename="downloaded.txt"');
echo " entire table here ";
?>
And then have your save button point to that pdf file
onclick="location.href='downloadTable.php?tableID=5';"
If you have a link on your page that points to a file that you are downloading from your site, and the browser doesn't recognize it, then you'll get the save as dialog appearing. Are there specific requirements that you have where you can't do this?

Categories