【问题标题】:PHP download file from server from POST从 POST 从服务器下载 PHP 文件
【发布时间】:2018-01-02 08:04:32
【问题描述】:

从这里开始:PHP finding file where post INCLUDES portion of filename

我正在尝试结束这个问题。

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

快速回顾一下,在用户输入航次编号并且数据表返回航次列表后,用户然后单击链接,这是我将开始代码的地方:

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

我尝试使用此处找到的答案:Download files from server php

但我不确定我应该把标题放在哪里,或者我是否需要使用它们。

【问题讨论】:

    标签: php jquery file download directory


    【解决方案1】:

    我可以建议您的快速解决方案是:如果文件存在则返回文件路径,如果文件不存在则返回 false。 之后在你的JS代码中你可以检查,如果你的“数据”== false,你可以抛出一个错误“文件不存在”,如果它不是“false”,你可以调用document.location.href =数据; - 它将您的浏览器重定向到该文件并被下载

    【讨论】:

      【解决方案2】:

      你为什么不简单地使用下载属性:

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

      如果您真的想使用 JavaScript 开始下载,请使用 style="display:none;" 替换 &lt;a&gt;,然后在 JS 中单击它:

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

      然后调用它:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-10-18
        • 1970-01-01
        • 1970-01-01
        • 2012-10-13
        • 1970-01-01
        • 2021-09-28
        相关资源
        最近更新 更多