PHP JQuery Ajax 在处理过程中更改按钮值 FTP 下载

PHP JQuery Ajax change button val during processing FTP download

我有一个 PHP table 网格,其中列出了一些需要不时手动下载的文件。我宁愿让 "download from ftp" 按钮显示正在发生的事情,而不是转到专门的页面来处理下载。我不需要百分比指标。我只是想让按钮的值改变3次,即:

为此,我使用 JQuery Ajax,但不知道执行此操作的最佳实践方法是什么。

这是按钮和 JQuery Ajax 代码:

<input id="submit_1" type="submit" value="download from ftp" name="submit_1">
    <script>
        $(document).ready(function(){
            $('#submit_1').click(function() {
                $.ajax({
                    type: 'POST',
                    url: 'ajax.php',
                    dataType: 'html',
                    data: {
                        a: 'ftp_download',
                        file_id: 1
                      },
                    success: function(txt){
                        $('#submit_1').val('download complete');
                    }
                });
                return false;
            });
        });
    </script>

ajax.php 将包含如下内容:

//#STEP 1 - Connect to FTP
             #set up basic connection
             $ftp_connection = ftp_connect($ftp_host,$ftp_port,$ftp_timeout);

             #login with username and password
             $login_result = ftp_login($ftp_connection, $ftp_username, $ftp_password);

//#STEP 2 - Download File                 
             #try to download $server_file_path and save to $local_file_path
             if (ftp_get($ftp_connection, $local_file_path, $server_file_path, FTP_BINARY)) {
      //#STEP 3 - Download complete
                return true;
             }else{
                return false;
             }

             #close the connection
             ftp_close($ftp_connection);

如何在处理 php 脚本时更改提交按钮的值? IE:连接到 FTP 时,显示 "Connecting to FTP"。在那一步之后,下载开始,此时按钮值可以变为 "downloading file"..

是否可以从 ajax.php 内更新按钮,还是需要在 <script>... $.ajax...</script> 部分完成?

我考虑过将 PHP 文件分成两部分(第一个用于测试 FTP 连接,然后用于下载文件),然后使用 2 个 Ajax 调用,一个用于连接到 FTP,然后如果成功,继续第 2 步(可能将成功消息从第 1 步传递到第 2 步,所以我不必在第 2 步中重新连接到 ftp)。但是我不要认为那是很好的编码实践。

我做了两个 php 文件:

  • 执行下载并将步骤记录到文件中的那个
  • 第二次检查文件,更新状态和清理。

在你的 javascript 中:

$(document).ready(function() {
    $('#submit_1').click(function() {
        $.ajax({
            type: 'POST',
            url: 'ajax.php',
            dataType: 'html',
            data: {
                a: 'ftp_download',
                file_id: 1
              },
            success: function(txt){

            }
        });
        checkStatus(1);
    });
});

function checkStatus(idFile)
{
    $.ajax({
        type: 'POST',
        url: 'check_status.php',
        dataType: 'JSON',
        data: {
            file_id: idFile
          },
        success: function(response) {
            $('#submit_1').val(response.message);
            if (response.done != true) {
                setTimeout("checkStatus(" + idFile + ")", 1000);
            }
        }
    });
}

PHP 正在下载:

<?php

$fileHash = md5($_POST['file_id']);

//#STEP 1 - Connect to FTP
file_put_contents($fileHash, json_encode(array('message' => 'Connecting', 'done' => false)));
$ftp_connection = ftp_connect($ftp_host,$ftp_port,$ftp_timeout);
if ($ftp_connection === false) {
    file_put_contents($fileHash, json_encode(array('message' => 'Connection error', 'done' => true)));
}
$login_result = ftp_login($ftp_connection, $ftp_username, $ftp_password);
if ($ftp_connection === false) {
    file_put_contents($fileHash, json_encode(array('message' => 'Login error', 'done' => true)));
}          

//#STEP 2 - Download File  
file_put_contents($fileHash, json_encode(array('message' => 'Downloading', 'done' => false)));
if (ftp_get($ftp_connection, $local_file_path, $server_file_path, FTP_BINARY)) {
    file_put_contents($fileHash, json_encode(array('message' => 'Download complete', 'done' => true)));
} else {
    file_put_contents($fileHash, json_encode(array('message' => 'Download error', 'done' => true)));
}
ftp_close($ftp_connection);

PHP check_status

<?php

$response = array('message' => 'Connecting', 'done' => false);
$fileHash = md5($_POST['file_id']);

if (file_exists($fileHash)) {
    $response = json_decode(file_get_contents($fileHash));
    if ((bool) $response->done === true) {
        // Clean the file
        unlink($fileHash);
    }
}

header('Content-Type: application/json');
echo json_encode($response);