Jump to content


Photo

Downloading file after form submission

file download form submissin submit

  • Please log in to reply
5 replies to this topic

#1 pawel

pawel

    Jr. Member

  • Members
  • PipPip
  • 12 posts
  • 0

Posted 23 May 2012 - 04:59 AM

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?

#2 ryan

ryan

    Hero Member

  • Administrators
  • 5,780 posts
  • 3124

  • LocationAtlanta, GA

Posted 23 May 2012 - 09:55 AM

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.

#3 Soma

Soma

    Hero Member

  • Moderators
  • 3,205 posts
  • 1757

  • LocationSH, Switzerland

Posted 23 May 2012 - 10:32 AM

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

@somartist | modules created | support me, flattr my work flattr.com


#4 Pete

Pete

    Administrator

  • Administrators
  • 1,756 posts
  • 658

  • LocationChester, England

Posted 23 May 2012 - 10:53 AM

Thanks for posting that Soma - invaluable for something I'm working on right now ;)

#5 ryan

ryan

    Hero Member

  • Administrators
  • 5,780 posts
  • 3124

  • LocationAtlanta, GA

Posted 23 May 2012 - 11:16 AM

Sorry, I mis-typed it, that should be "filename" not "pathname". I updated the example.

#6 thijs

thijs

    Jr. Member

  • Members
  • PipPip
  • 24 posts
  • 8

  • LocationAmsterdam, The Netherlands

Posted 01 September 2012 - 09:41 AM

Thanks for the download class Soma, comes in very handy!





Also tagged with one or more of these keywords: file, download, form, submissin, submit

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users