Php download file in background

We use wordpress and have our users fill in a form. When they click on the submit button, the following happens [thanks to a PDF plugin]:

  1. The form and the submit button disappear
  2. A green message is shown ["Success..."] and a download link to a PDF file appears, that the plugin has generated out of the filled form so the user can download it.

However, the plugin does not place the file on our servers and thanks to the nonce system [it's like a token], the link won't work at a later point in history.

And that's why I want to auto-download the file to the server as well [no matter if the user downloads it too or not].

My plan so far: Tie a jQuery script to the submit button, that [as soon as the button is clicked] waits for an element [in this case the download link] to appear like so:

$[document].ready[function[]{
    $[".submit-button"].click[function [] {
        waitForEl[".pdf-link", function[] {
            console.log[".pdf-link"].attr["href"];
        }];
    }];
}];

var waitForEl = function[selector, callback] {
    if [jQuery[selector].length] {
      callback[];
    } else {
      setTimeout[function[] {
        waitForEl[selector, callback];
      }, 100];
    }
};

[Source of the waitForEl function: //gist.github.com/chrisjhoughton/7890303]

Where you see the console.log, I want some way to automatically download this PDF file to the server of our website instead, next to the JS file where the code from above is in. Is that even possible? I can use JS and PHP to achieve this somehow, since we're using Wordpress.

View Discussion

Improve Article

Save Article

  • Read
  • Discuss
  • View Discussion

    Improve Article

    Save Article

    This post deals with creating a start downloading file using PHP. The idea is to make a download button which will redirect you to another page with the PHP script that will automatically start the download.

    Creating a download button:

        

        

            .btn {

                background-color: limeGreen;

                border: none;

                color: white;

                padding: 12px 30px;

                cursor: pointer;

                font-size: 20px;

            }

            .btn:hover {

                background-color: green;

            }

        

        

            Auto width:

            

                Download

            

            Full width:

            

                Download

            

        

    Output:

    To redirect to some file which has the file to be downloaded, create an HTML form as shown below.

     

    Output:

    PHP code to download: When the user clicks the above button, the code will be redirected to the “downloadFile.php” file. Now, use the URL of the file and PHP file_get_contents[] function to download the file.

    Output:


    Chủ Đề