2

What began here: PHP finding file where post INCLUDES portion of filename

I am trying to finish with this question.

Basically, now that I am able to post a variable to a PHP process, then use that process to find a file in a directory, I now need to be able to download the file if it exists.

Quick recap, after the user has entered a voyage number and the datatable has returned a list of voyages, the user then clicks the link, which is where I'll begin the code:

$('.voyageFileCall').on('click', function()
{
  var voyage = $(this).attr('data-voyage');
  $.post('fileDownload.php', {voyage:voyage}, function(data)
  {
    // here is where I need to where either display the file doesn't exist
    // or the file downloads
  });
});

The process 'fileDownload.php' looks like this:

<?php
  if($_POST['voyage'] == true)
  {
    $voyage = $_POST['voyage'];
    $files = scandir("backup/");
    if(count($files) > 0)
    {
      $fileFound = false;
      foreach($files as $file)
      {
        if((preg_match("/\b$voyage\b/", $file) === 1))
        {
          // I'm guessing the download process should happen here
          echo "File found: $file \n"; // <-- this is what I currently have
          $fileFound = true;
        }
      }
      if(!$fileFound) die("File $voyage doesn't exist");
    }
    else
    {
      echo "No files in backup folder";
    }
  }
?>

I tried to use the answer found here: Download files from server php

But I'm not exactly sure where I should put the headers, or if I need to use them at all.

halfer
  • 19,824
  • 17
  • 99
  • 186
John Beasley
  • 2,577
  • 9
  • 43
  • 89

2 Answers2

0

The quick solution which i can suggest you is: return path to file if it is exist, and return false if file doesn't exist. After that in your JS code you can check, if your "data" == false, you can throw an error "file doesn't exist", and if it is not "false", you can call document.location.href = data; - it will redirect your browser to the file and it will be downloaded

Denis Gorgul
  • 79
  • 1
  • 3
0

Why don't you simply use download attribute:

<?php
  if($_POST['voyage'] == true)
  {
    $voyage = $_POST['voyage'];
    $files = scandir("backup/");
    if(count($files) > 0)
    {
      $fileFound = false;
      foreach($files as $file)
      {
        if((preg_match("/\b$voyage\b/", $file) === 1))
        {
          // I'm guessing the download process should happen here
          echo 'File found: <a href="' . $file . '" download>' . $file . '</a> \n'; // <-- this is what I currently have
          $fileFound = true;
        }
      }
      if(!$fileFound) die("File $voyage doesn't exist");
    }
    else
    {
      echo "No files in backup folder";
    }
  }
?>

If you really want to use JavasScript to start download then use style="display:none;" for <a> and then in JS just click it:

echo 'File found: <a id="myDownload" style="display:none;" href="' . $file . '" download>' . $file . '</a> \n';

and call it:

  $('.voyageFileCall').on('click', function()
    {
      var voyage = $(this).attr('data-voyage');
      $.post('fileDownload.php', {voyage:voyage}, function(data)
      {
if(document.getElementById("myDownload")){
document.getElementById("myDownload").click();
}else{
console.log("file does not exist");
}
      });
    });
Djordje Vujicic
  • 414
  • 5
  • 20