【问题标题】:How to delete file after access it with ajax request?使用ajax请求访问文件后如何删除文件?
【发布时间】:2020-04-05 19:34:43
【问题描述】:

我将我的站点文件保存在 ftp 服务器中。在以下代码中,用户可以通过 ajax 请求访问 ftp 文件。

HTML

<a onclick="attach('354.tif')"><i class="fa fa-paperclip" aria-hidden="true"></i></a>

JavaScript

function attach(fileLink) {
var xmlhttp = new XMLHttpRequest();
var ajaxResponse;
xmlhttp.onreadystatechange = function() {
    if (this.readyState == 4 && this.status == 200) {
        ajaxResponse = this.responseText;
        var response = JSON.parse(ajaxResponse);
        if(response.location){
            window.location.href = response.location;
        }
    }
};
xmlhttp.open("GET", "/ajax/ftp-download.php?q=" + fileLink, true);
xmlhttp.send();
}

ftp-download.php

<?php
define('__ROOT__',dirname(dirname(__FILE__)));
require_once __ROOT__.'/config/ftpconfig.php'; 
require_once __ROOT__.'/functions/file-process/tiff-to-pdf.php';    
$ftp_file = '/'.$_REQUEST["q"];
$ftp_file_ext = pathinfo($ftp_file, PATHINFO_EXTENSION);

// set up basic connection
$conn_id = ftp_connect($ftp_server, $ftp_port) or die(err_handler(ftp_connect_error)); 

// login with username and password
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);
ftp_pasv($conn_id, true);

$downloaded_file = $_SERVER['DOCUMENT_ROOT']."/user-files/".mt_rand(1000, 999999).".".$ftp_file_ext;

// try to download $ftp_file and save to $local_file
if (ftp_get($conn_id, $downloaded_file, $ftp_file, FTP_BINARY)) {
    if (strcasecmp($ftp_file_ext,"tif") == 0 or strcasecmp($ftp_file_ext,"tiff") == 0) {
        $output_file = "/user-files/".convert_tif_to_pdf($downloaded_file);     
    } else {
        $output_file = "/user-files/".basename($downloaded_file).PHP_EOL;
    }
    header('Content-Type: application/json');
    echo json_encode(['location'=>$output_file]);
} 
// close the connection
ftp_close($conn_id);
?>

我想知道在用户访问文件后是否可以删除 $downloaded_file$output_file

谢谢。

【问题讨论】:

    标签: php ajax ftp


    【解决方案1】:

    您无需将文件保存到网络服务器上的临时物理文件中。

    您可以直接发送给客户端。

    Download file via PHP script from FTP server to browser with Content-Length header without storing the file on the web server

    【讨论】:

      【解决方案2】:

      谢谢马丁,终于我在你的帮助下解决了我的问题。 我想分享我的代码,也许它会帮助其他人。

      使用以下代码,您可以从 ftp 服务器下载文件,并使用 PHP Imagick 类将非 PDF 文件转换为 PDF 并在浏览器窗口中查看文件

      HTML 元素

      <a onclick="attach('354.tif')"><i class="fa fa-paperclip" aria-hidden="true"></i></a>
      
      <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
      <!-- jQuery Modal -->
      <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.0.0/jquery.min.js"></script>
      <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/jquery-modal/0.9.1/jquery.modal.min.css" />
      

      新建 ftp-download.php 文件

      <?php
      
          $ftp_file = "/".$_REQUEST["q"];
          $ftp_file_ext = pathinfo($ftp_file, PATHINFO_EXTENSION);
      
          $pdfFile = get_ftpfile_blob($ftp_file);
      
      
          if (strcasecmp($ftp_file_ext, 'pdf') != 0) {
              $pdfFile = convert_tif_to_pdf($pdfFile);    
          }
      
      function get_ftpfile_blob($file) {
      
          require_once $_SERVER['DOCUMENT_ROOT'].'/config/ftpconfig.php'; // this file contain ftp configuration
      
              $conn_id = ftp_connect($ftp_server, $ftp_port); 
              ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);
              ftp_pasv($conn_id, true);
      
              ob_start();
              $result = ftp_get($conn_id, "php://output", $file, FTP_BINARY);
              $data = ob_get_contents();
              ob_end_clean();
      
              ftp_close($conn_id);
      
              if ($result) {
                  return $data;
              } else {
                  die('');
              }
      }
      
      function convert_tif_to_pdf($inputFile) {
          $document = new Imagick();
          $document->readImageBlob($inputFile);
          $document->setFormat("pdf");
          return $document->getImagesBlob();  
      }
      
          echo base64_encode($pdfFile);
          die();
      ?>
      

      新 jquery

      function attach(fileLink) {
          $.get("/ajax/ftp-download.php", 
              {
                  q: fileLink
              } , 
              function(data){
                  // Display the returned data in browser
                  if (data == "") {
                      var modalWindow = '<div id="saveMsg" class="modal">' +
                                        '<p>File Not Found!</p>' +
                                        '<a href="#" rel="modal:close"></a>' +
                                        '</div>';
                      $('body').append(modalWindow);
                      $('#saveMsg').modal({fadeDuration: 100});
                      return;
                  }
                  var link = document.createElement('a');
                  link.href = 'data:application/pdf;base64,' + data;
                  document.body.appendChild(link);
                  window.open(link);
              }
          );
      }
      

      【讨论】:

        猜你喜欢
        • 2015-04-03
        • 2019-12-31
        • 2022-08-16
        • 2022-01-23
        • 1970-01-01
        • 2023-03-09
        • 2014-07-29
        • 1970-01-01
        • 2015-07-30
        相关资源
        最近更新 更多