Jump to content

Downloading file after form submission


pawel
 Share

Recommended Posts

Hi y'all!

Im am trying to force a file download after user submits a form. When user hits submit button POST data are send to a template (sort of "thank you for downloading" page). This template on successful submission redirects to another template through this code

<?php
$downloadurl = $pages->get('/download/')->url;
header('refresh: 0; url='.$downloadurl);
?>

Redirection then hits this template code (located in download template):

<?php
// filename & url of the pdf file
$filename = $pages->get('/some-page/')->file->name;
$fileurl = $pages->get('/some-page/')->file->url;
$filesize = $pages->get('/some-page/')->file->filesize;

// force download of the file
header('Content-type: application/pdf');
header('Content-Disposition: attachment; filename='.$filename);
header("Content-Length: ".$filesize);
readfile($fileurl);

It downloads a file and then redirects back to thank you page. The only problem is that the downloaded file is empty (0KB). When i download a file "the standard way" clicking on a link that directs to $pages->get('/some-page/')->plik->url; everything is ok.

Any ideas?

Link to comment
Share on other sites

You need to give readfile() the actual server file path rather than the URL:

$filename = $pages->get('/some-page/')->file->filename; 
...
readfile($filename); 

Also, I think it would be preferable if you could find a way to do this without all the redirects. At least, if switching to $pathname doesn't fully resolve the problem, I would look at the redirects first.

Link to comment
Share on other sites

You need to give readfile() the actual server file path rather than the URL:

$pathname = $pages->get('/some-page/')->file->pathname;

Ryan, this doesn't return the anything for me. ?

echo $page->pdf->pathname; // returns nothing

However, I got a little class that works pretty well for download files. Without getting headache.

https://gist.github.com/2775882

  • Like 1
Link to comment
Share on other sites

  • 3 months later...

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...