title: How PHP Can Perform a Background Task?
body: <p>To try a background test, I create 3 files:</p>
  
  <p><strong>Index.html</strong> (Responsible for calling a php file by ajax)</p>
  
  <pre><code>&lt;!DOCTYPE html&gt;
  &lt;html&gt;
  &lt;head&gt;
  &lt;meta charset="UTF-8"&gt;
  &lt;title&gt;Background Task Manager&lt;/title&gt;
  &lt;script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"&gt;&lt;/script&gt;
  &lt;/head&gt;
  
  &lt;body&gt;
  
  Press The Button To execute a Background Task...
  &lt;br&gt;
  &lt;button id="perform"&gt;Perform Task&lt;/button&gt;
  
  &lt;/body&gt;
  
  &lt;script&gt;
  
  $( document ).ready(function() {
  
      $( "#perform" ).click(function() {
        submitAjax();
      });
  
      function submitAjax() {
              $.ajax({
                  url: 'test.php',
                  type: "post",
                  data: '',
                  success: function (data) {
                      alert(data);
                  }
              });
          }
  
  });
  
  &lt;/script&gt;
  
  &lt;/html&gt;
  </code></pre>
  
  <p><strong>Test.php</strong> (The file who call another file using a background method)</p>
  
  <pre><code>&lt;?php
  
  //Perform Background Task
  
  exec("C:/wamp/bin/php/php5.6.35/php.exe C:/wamp/www/background/file.php");
  
  echo "Process Started";
  
  ?&gt;
  </code></pre>
  
  <p><strong>File.php</strong> (The file that will be executed in background)</p>
  
  <pre><code>&lt;?php
  
  //Create a File
  
  sleep(20);
  
  $content = "My Text File";
  $fp = fopen("myText.txt","wb");
  fwrite($fp,$content);
  fclose($fp);
  
  echo "File Created...";
  
  ?&gt;
  </code></pre>
  
  <p><strong>The idea would be as follows:</strong> Once the user clicks the button, a request is made to the test.php file. The test.php will trigger a background request for the file.php, the message ('Process Started') will appear immediately, and 20 seconds after a file would be created in my project folder.</p>
  
  <p><strong>What is happening:</strong> When the user clicks the button, I only receive the message 'Process Started' 20 seconds later, ie the request is not being made in background mode.</p>
  
  <p><strong>What I wish to happen:</strong> When the user clicks the button, the message 'Process Started' will appear immediately, and 20 seconds later php will create the file in the folder of my project.</p>
  
  <p>How can I solve this problem ?</p>