Forum

Thread tagged as: Question, Problem

Download file behind form

Hi there,

I have a client who wants some simple PDF files behind a form, as he wants to collect emails. I have made a list/detail template where he can add the files and some information. I've also made the form that will stand in front of the file. I know I can redirect a form upon submission but is it possible to send the user to a page where I could carry over the path to the download and serve it up for download or trigger the download on the dame page but only once the form has submitted?

Mathew Doidge

Mathew Doidge 1 points

  • 1 month ago

I've just done this. It was for a site where the user needs to fill in their name, company and email address before they can access the report:

if (perch_post('name') && perch_post('company') && perch_post('email')) {
    $reportLink = perch_blog_custom([
      'blog'          => 'reports',
      'count'         => 1,
      'template'      => '_report_link',
      'filter'        => 'postSlug',
      'match'         => 'eq',
      'value'         => perch_get('s'),
      'skip-template' => true,
      'return-html'   => true,
    ]);
    PerchSystem::redirect($reportLink['html']);
  }

Hi Toby,

So it looks like you're redirecting them to a blog post, in your case a 'report'? Do you think this concept could deliver a file, rather than view something?

This redirects them to the pdf. The user can then decide whether to download it or just view it.

For context here is the _report_link template I'm using to get the url of the PDF:

<perch:blog id="report" type="file" label="Report PDF" bucket="Reports" />

Here's the test site, feel free to fill in the form to test, the email just comes to me at the mo:

http://frukt.creativemonster.co.uk/insights/design-flavours-1

Thanks to Toby, I have a working solution where by a user completes the single field form and gets taken directly to the pdf in the browser.

I tweaked the code to try and force a download in the browser, which I have working. I am just wondering if it's possible to still get the form to show the success message as well as download the file? At the moment if you complete the form, you see the download prompt appear but no success message - I am guessing because I am firing off those new headers.

My code:

if (perch_post('email_addr')) {
    $fileLink = perch_content_custom('Downloads', [
          'count'         => 1,          
          'filter'        => 'download_slug',
          'match'         => 'eq',
          'value'         => perch_get('s'),          
          'skip-template' => true,          
    ]);

    $fileName = perch_content_custom('Downloads', [
          'count'         => 1,          
          'filter'        => 'download_slug',
          'template'      => 'file_name.html',
          'match'         => 'eq',
          'value'         => perch_get('s'),          
          'skip-template' => true,          
    ]);

    // File path to check.
    $filePath = $_SERVER['DOCUMENT_ROOT'] . $fileLink[0]['download_file'];

    // If file exists
    if(file_exists($filePath)) {
        $fileName = basename($filePath);
        $fileSize = filesize($filePath);

        // Output headers.
        header("Cache-Control: private");
        header("Content-Type: application/stream");
        header("Content-Length: ".$fileSize);
        header("Content-Disposition: attachment; filename=".$fileName);

        // Output file.
        readfile ($filePath);                   
        exit();
    }
    else {
        die('The provided file path is not valid.');
    }
}