简体   繁体   English

为什么我的PHP脚本间歇性地无法通过FTP上传文件?

[英]Why is my PHP script intermittently unable to upload a file via FTP?

I wrote a PHP script to export orders from an e-commerce store, save them in an XML file and then copy them to a remote FTP folder. 我编写了一个PHP脚本来从电子商务商店导出订单,将它们保存在XML文件中,然后将它们复制到远程FTP文件夹。 Another company monitor that FTP folder for new files and use them to fulfil the orders. 另一家公司监控该FTP文件夹以获取新文件并使用它们来完成订单。

For some reason the script only uploads the file some of the time and I am finding debugging / getting to the bottom of this very difficult. 出于某种原因,脚本只会在某些时候上传文件,而我正在寻找调试/到达这个非常困难的底部。 This is the main part of the offending script: 这是违规脚本的主要部分:

if ($ftp_conn = ftp_connect($ftp_server)) {
    if ($login = ftp_login($ftp_conn, $ftp_user, $ftp_pass)) {

        $filename = time() . '-orders.xml';
        ftp_pasv($ftp_conn, true);

        if (ftp_put($ftp_conn, $filename, $xmlPath, FTP_ASCII)) {
            $logString .= ' - Successfully run';
            ftp_close($ftp_conn);
        } else {
            $logString .= ' - Failed to upload file';
            $error = error_get_last();
            $logString .= ' - ' . $error['message'];
            ftp_close($ftp_conn);
        }
    } else {
        $logString .= ' - Failed to log in to server';
        ftp_close($ftp_conn);
    }
} else {
    $logString .= ' - Failed to connect to server';
};

I save the $logString variable to a text file. 我将$ logString变量保存到文本文件中。 The only error I actually see reported is: 我实际看到的唯一错误是:

ftp_put(): Type set to A

I can't find anything online about what that means or if it should be causing the problem. 我无法在网上找到任何关于这意味着什么或是否应该导致问题的内容。

The FTP connection log is no help either. FTP连接日志也没有帮助。 This is all you see when it fails to run: 这是你无法运行时看到的全部内容:

"PASS (hidden)" 230 -
"TYPE A" 200 -

Sometimes it works, sometimes it doesn't. 有时候它有效,有时则不然。 There's no obvious pattern to when it fails. 它失败时没有明显的模式。 Large and small XML files have worked and failed. 大型和小型XML文件都有效且失败。

Apparently the server can handle 3 simultaneous connections and in theory there should only ever be 2 so I can't see that being the issue. 显然,服务器可以处理3个同时连接,理论上应该只有2个,所以我不能看到这个问题。

Is the PHP script OK and, if so, is there anything else I can do to try and log what happens when the file fails to upload? PHP脚本是否正常,如果是这样,我还能做些什么来尝试记录文件无法上传时会发生什么?

Whether this is considered a fix or a bodge job, I will leave up to you. 无论这被视为修复还是躲避工作,我都会留给您。

My best guess is there is an issue with the 1and1 FTP server I am connecting to as the script above worked without issue on a different server. 我最好的猜测是我连接的1and1 FTP服务器存在问题,因为上面的脚本在其他服务器上没有问题。

I tried switching the ftp_put mode to FTP_BINARY but it made no difference. 我尝试将ftp_put模式切换到FTP_BINARY,但没有区别。

I got around the issue by looping the whole function until it was completed successfully. 我通过循环整个函数来解决这个问题,直到它成功完成。 Ie: 即:

$uploaded = false;
$tries = 0;
$filename = time() . '-orders.xml';

while (!$uploaded && $tries <= 5) {
    $tries++;
    if ($ftp_conn = ftp_connect($ftp_server)) {
        if ($login = ftp_login($ftp_conn, $ftp_user, $ftp_pass)) {
            $logString .= ' - Attempt ' . $tries;

            ftp_pasv($ftp_conn, true);
            ftp_set_option($ftp_conn, FTP_TIMEOUT_SEC, 120);

            if (ftp_put($ftp_conn, $filename, $xmlPath, FTP_BINARY)) {
                $logString .= ' - Successfully run';
                $uploaded = true;
                ftp_close($ftp_conn);
            } else {
                $logString .= ' - Failed to upload file';
                $error = error_get_last();
                $logString .= ' - ' . $error['message'];
                ftp_close($ftp_conn);
            }
        } else {
            $logString .= ' - Failed to log in to server';
            ftp_close($ftp_conn);
        }
    } else {
        $logString .= ' - Failed to connect to server';
    };
};

I capped $tries at 5 in case something was genuinely wrong. 如果出现真正错误的情况,我将5美元试探上限。 The CRON job runs often enough that if it doesn't work one time it can work another. CRON作业经常运行,如果它不能工作一次,它可以工作另一个。

So far this has worked well. 到目前为止,这一点运作良好。 The most tries needed to upload a file has been 4 and at no point has it completely failed to upload the file. 上传文件所需的尝试次数最多为4次,并且完全无法上传文件。 So it's working and I can relax a bit. 所以它正在工作,我可以放松一下。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM