How to trigger a download prompt through post callback function - php

simple question I guess, but can't figure it out... Here's the problem:
In the front end, I export some user-selected data to the server to dynamically create a file:
$("#export-button").click(function() {
$.post("'.$postUrl.'",{variable:exportSelection},
function(data) {
console.log(data);
}
);});
Then, after I receive the data and create/save the file on the server, I am doing the following in php:
if (file_exists($file)) {
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename($file));
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
ob_clean();
flush();
readfile($file);
exit;}
What am I supposed to do in the post callback function instead of printing on the console in order to get a download prompt?
UPDATE: OK - I just realised that my #export-button was an anchor tag with no href... Now when I point to the file on the server, the problem is that when I click, it follows the link prompts to save file etc., but it gets to the file BEFORE the "new" version is generated (so using the previous selection at each click)

OK - I figured it out. I just needed to disable the link from pointing anywhere and then do:
window.location = 'my_file_location.doc';
... inside the callback function

Related

Incorrect Headers?

What I'm trying to do:
Im trying to push a jpg file to download witout user seeing the URL. In this case the file is located at: http://www.example.com/upload/asdasdsadpokdaspdso/36.jpg.
My current code:
header('Content-type: application/octet-stream');
header('Content-Disposition: attachment; filename="'.$download->name.'.jpg"');
readfile($weburl."/upload/".$hiddenpassage."/".$download->link);
My vars / db values:
$weburl = "http://wwww.example.com";
$hiddenpassage = "asdasdsadpokdaspdso";
$download->link = 36.jpg //not a var, just drom db.
$download->name = The First Test Product //not a var, just from db.
The problem:
When I get the download I open it and I get the following error:
The file “The First Test Product (28).jpg” could not be opened.
It may be damaged or use a file format that Preview doesn’t recognize.
Renaming .jpg to .txt:
http://pastebin.com/K9NGL5RP
Most of that is the content of the page I downloaded it from.
I think you need to specify the whole header (untested). Specially the Content-Length.:
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.$download->name.'.jpg');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($weburl."/upload/".$hiddenpassage."/".$download->link));
readfile($weburl."/upload/".$hiddenpassage."/".$download->link);
Hope this helps.

Render view after image file is downloaded in Yii

public function actionViewDownload(){
// some in internal processing php commands (no echo)
exec($command); // command to be executed compulsary
$file = "/images/sample.jpg"; // some images file
if (file_exists($file)) {
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename($file));
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
readfile($file);
}
$this->render('view',array('data'=>$data)); // render the other view of the controller after/during download.
}
I need to execute a command, then download the image file and after or during downloading render, the view.
If I render the view before downloading it prompts "Header cannot be modified. Headers already sent"
and if I render the view after the download then view is now shown on browser but the file gets downloaded.
My question here is that how can I achieve three tasks : execution of the command (this must be first), render and download.
By the looks of it you are trying to do something like sourceforge does when you ask to download a project where the deliverable is pushed to the user as the page is rendered.
I am assuming that the exec generates the image.
You really need to split this into 2 actions:
public function actionViewDownload(){
// some in internal processing php commands (no echo)
exec($command); // command to be executed compulsory
$file = "/images/sample.jpg"; // some images file
$this->render('view',array(
'data'=>$data,
'img'=>$file)
);
}
Then the view contains something like:
<img src="<?php echo controller/getdeliverible?file=$img; ?>"/>
which in turn calls:
public function actionGetDeliverible($file){
if (file_exists($file)) {
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename($file));
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
readfile($file);
}
}
My above code is rough (you'll have to fill it in properly) but you get the idea. Basically as the view is rendered the image will pop up and ask you to save it. It will appear to the user that both things are happening together.
The nice thing about doing it this way is that you can do something after you have read the image to the user like delete it if it was a temporary file so it can't be downloaded again.

PHP download/redirect

Okay so this is how I rewrote it. Any changes? Will this work? I've added the Javascript at the end with the timeout.
if (file_exists($file)) {
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename($file));
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
ob_clean();
flush();
readfile($file);
}
?>
<script type="text/Javascript">
window.setInterval(function()
{
window.location = "www.moneymovers.com";
}, 5000);
</script>:
You can't download and then do a header redirect. The client is going to redirect as soon as it sees the header, which means the download won't work. But you also can't output the header after the download, because then the redirect won't happen. Either way, it's not gonna work.
I would recommend doing this at a different level. For example, when the user clicks the download link, have some JavaScript start the download in a new tab/window and then redirect to the desired location in the current tab.
You can put the location header wherever you want, as long as nothing has been outputted (echo).
If you have outputted something, you can output something like this to redirect :
<script type="text/javascript">
window.location = "www.example.com";
</script>
EDIT :
In your case, it is not possible to do what you're looking for, as far as I know. You will have to manage this case in the caller page. The Javascript will not be called because you've modified the Content-Disposition.

ExtJS grid form with document in MySQL

I have a grid whith the data that come from a database. Each row of the grid containt these fields : document name, owner, version etc. The content of the document is in a blob.
I want, when the user double-click on a row, to download the content of the document. The best way will be a pop-up that ask a confirmation. The grid must stay on the screen.
I'm able to read the content of the document with an ajax request and put the result in a variable.
Ext.Ajax.request({
url: '../controleurs/c_get_doc_content.php',
method: 'POST',
params: { dep: r.data.nom_document },
success: function(response, opts) {console.log(response);console.log(opts)},
requestcomplete: function (conn, response, options) {},
failure: function() {console.log('failure');},
headers: { 'my-header': 'foo' },
How can I save the content of the document to a file on the user PC ?
The best solution for this is server side. Instead of passing the blob to the JS, you just pass an ID. Then when the user chooses to download the file, you call the server and it returns a document that can be downloaded.
You would just stream that content as an attachment instead of sending it with the AJAX request
in PHP you use the following code to make sure a file is downloaded and the save as dialog is displayed.
if (file_exists($file)) {
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename($file));
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
ob_clean();
flush();
readfile($file);
exit;
}
Set the content type of your PHP response to something the browser would want to save....

Creating a download link for .jpg file using PHP

This one should be easy, I think. I have a paginated image gallery, and under each image is a small link that says "Download Comp". This should allow people to quickly download the .jpg file (with a PHP generated watermark) to their computer.
Now, I know I can just link straight to the .jpg file, but that requires the user to have the image open in a new window, right click, Save As..., etc. Instead, I want the "Download Comp" link to initiate the download of the file immediately.
PHP.net seemed to suggest using readfile(), so each "Download Comp" link is being echoed as "?download=true&g={$gallery}&i={$image}".
Then at the top of the page I catch to see if the $_GET['download'] var isset, and if so, I run the following code:
if(isset($_GET['download'])) {
$gallery = $_GET['g'];
$image = $_GET['i'];
$file = "../watermark.php?src={$gallery}/images/{$image}";
header('Content-Description: File Transfer');
header('Content-Type: application/jpeg');
header('Content-Disposition: attachment; filename='.basename($file));
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: public');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
ob_clean();
flush();
readfile($file);
}
The link takes a lonnnnnnnnng time, and then it brings up a dialog prompt asking you to Open or Save the file, but once you Save and try to open it, it says the file is corrupt and can't be opened.
Any ideas?
Don't set $file to a relative url. The readfile function will try to access the php file on the server. That is not what you want. In your case it looks like the watermark.php file will send the contents you want, so you could possibly just set up the environment it needs and include it.
<?php
if(isset($_GET['download'])) {
$gallery = $_GET['g'];
$image = $_GET['i'];
$_GET['src'] = "{$gallery}/images/{$image}";
header('Content-Description: File Transfer');
header('Content-Type: image/jpeg');
header('Content-Disposition: attachment; filename='.basename($image));
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: public');
header('Pragma: public');
ob_clean();
include('../watermark.php');
exit;
}
Another (simpler) way is to modify watermark.php. Add a query parameter to make it send the proper headers to force a download and link to that
...
watermark.php:
<?php
if (isset($_GET['download']) && $_GET['download'] == 'true') {
header('Content-Description: File Transfer');
header('Content-Type: image/jpeg');
header('Content-Disposition: attachment; filename='.basename($src));
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: public');
header('Pragma: public');
}
// continue with the rest of the file as-is
Also, you don't need the call to flush(). There should not be any output to send at that point, so it is not necessary.
header('Content-Type: image/jpeg');
Perhaps?
I think you might need to follow the call to readfile() with a call to exit() to make sure nothing else gets written to the output buffer.
This seems like a security issue.
What if someone enters:
$g = '../../../../../../';
$i = '../../sensitive file at root';
How about making .htaccess (if you are using apache) i for the gallery directory serve jpegs up as a download rather than normal.
Also, try file_get_contents() instead of readfile(). I find it works under more circumstances. I would also recommend you use ob_flush() after you output the image data. I've never needed to use ob_clean() or flush() to get this kind of thing to work.
And as Eric said, you may also want to put a call to exit() in there as well for good measure if it still isn't working just in case you are getting some junk data stuck at the end.

Categories