Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/297.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/68.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从POST服务器下载PHP文件_Php_Jquery_File_Download_Directory - Fatal编程技术网

从POST服务器下载PHP文件

从POST服务器下载PHP文件,php,jquery,file,download,directory,Php,Jquery,File,Download,Directory,从这里开始: 我想把这个问题讲完 基本上,现在我能够将变量发布到PHP进程,然后使用该进程在目录中查找文件,现在我需要能够下载文件(如果存在) 快速回顾,在用户输入航次号并且datatable返回了一个航次列表后,用户然后单击链接,我将从这里开始代码: $('.voyageFileCall').on('click', function() { var voyage = $(this).attr('data-voyage'); $.post('fileDownload.php', {voy

从这里开始:

我想把这个问题讲完

基本上,现在我能够将变量发布到PHP进程,然后使用该进程在目录中查找文件,现在我需要能够下载文件(如果存在)

快速回顾,在用户输入航次号并且datatable返回了一个航次列表后,用户然后单击链接,我将从这里开始代码:

$('.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
  });
});
“fileDownload.php”过程如下所示:

<?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";
    }
  }
?>

我可以建议您的快速解决方案是:如果文件存在,则返回文件路径;如果文件不存在,则返回false。

然后在JS代码中检查,如果“数据”==false,您可以抛出错误“文件不存在”,如果不是“false”,您可以调用document.location.href=data;-它会将您的浏览器重定向到该文件并将其下载

为什么不直接使用“下载”属性:

<?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";
    }
  }
?>
并称之为:

  $('.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");
}
      });
    });
  $('.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");
}
      });
    });