0

我正在运行CENTOS 6.3,并且PHP 5.4.9使用PHP FTP commands. 我正在使用它来将大文件从一个域上传到同一服务器上的 FTP 帐户。

我注意到即使是小文件的上传也需要很长时间才能上传:大约 10 到 15 秒。他们最终会上传。如果我使用相同的脚本上传到不同的服务器,它会快速上传:不到一秒钟。

我可以在不同的服务器上使用相同的 FTP 凭据,并且它们上传速度很快。为什么来自同一服务器上的一个域的 FTP 速度很慢?

用脚本更新:

$ftp_server = "HOST NAME";
$ftp_user_name = "USERNAME";
$ftp_user_pass = "PASSWORD";
$ftp_folder = "/FTP DIRECTORY";
$path = "/testing/";
$file_name = $_FILES["theFile"]["name"];
$source_file = $_FILES["theFile"]["tmp_name"];
$destination_file = $ftp_folder.$path.$file_name;
$destination_path = $ftp_folder.$path;


function uploadFTP($ftp_server, $ftp_user_name, $ftp_user_pass, $source_file, $destination_file, $destination_path) {
    $conn_id = @ftp_connect($ftp_server); // set up basic connection
    $login_result = @ftp_login($conn_id, $ftp_user_name, $ftp_user_pass); // login with username and password
    if ((!$conn_id) || (!$login_result)) { // check connection 
        return false;
    } else {
        $check = @ftp_chdir($conn_id, $destination_path); //check to see if folder is there
        if ($check) { 
            $upload = ftp_put($conn_id, $destination_file, $source_file, FTP_BINARY); // upload the file
            if (!$upload) { // check upload status
                return false;
            } else {
                return true;
            }
        } else { 
            $check = @ftp_mkdir($conn_id, $destination_path); //make new folder
            if ($check) {
                $upload = @ftp_put($conn_id, $destination_file, $source_file, FTP_BINARY); // upload the file
                if (!$upload) { // check upload status
                    return false;
                } else {
                    return true;
                }
            } else {
                return false;
            }
        }

    }
    @ftp_close($conn_id); //close ftp
}
4

1 回答 1

1

如果它们在同一台服务器上,为什么不直接复制文件而不是将它们发送出去,然后再将它们带回?我认为您正在使用出站和入站连接阻塞端口 21 (FTP)。

::编辑::

我知道这不是代码审查,但这里有一些小的改进:

$ftp_server = "HOST_NAME";
$ftp_user_name = "USERNAME";
$ftp_user_pass = "PASSWORD";
$ftp_folder = "/FTP_DIRECTORY";
$path = "/testing/";
$file_name = $_FILES["theFile"]["name"];
$source_file = $_FILES["theFile"]["tmp_name"];

/**
 * OLD CODE:
 * $destination_file = $ftp_folder . $path . $file_name;
 * $destination_path = $ftp_folder . $path;
 * 
 * NEW CODE:
 * $destination_path = $ftp_folder . $path;
 * $destination_file = $destination_path . $file_name;
 * 
 * REASON FOR CHANGE: Saves you 1 concat, also, 1 less use of ftp_folder and path
 */
$destination_path = $ftp_folder . $path;
$destination_file = $destination_path . $file_name;


function uploadFTP($ftp_server, $ftp_user_name, $ftp_user_pass, $source_file, $destination_file, $destination_path) {
    /**
     * OLD CODE: 
     * $conn_id = @ftp_connect($ftp_server); # set up basic connection
     * 
     * REASON FOR CHANGE: There are times to suppress errors, this is not one of them.
     */ 
    $conn_id = ftp_connect($ftp_server) or die('Couldn\'t connect to ' . $ftp_server); # set up basic connection

    /**
     * REASON FOR NO CHANGE: ftp_login throws a warning on failure.
     */
    $login_result = @ftp_login($conn_id, $ftp_user_name, $ftp_user_pass); # login with username and password

    if (empty($conn_id) || empty($login_result)) { # check connection 
        return false;
    } else {
        /**
         * OLD CODE:
         * $check = @ftp_chdir($conn_id, $destination_path)
         * 
         * REASON FOR CHANGE: $check is redundant
         */
        if (@ftp_chdir($conn_id, $destination_path)) { # check to see if folder is there
            /**
             * OLD CODE:
             * $upload = @ftp_put($conn_id, $destination_file, $source_file, FTP_BINARY);
             * 
             * REASON FOR CHANGE: $upload is redundant
             */
            if (ftp_put($conn_id, $destination_file, $source_file, FTP_BINARY)) { # upload the file & check upload status
                return true;
            } else {
                return false;
            }
        } else { 
            /**
             * OLD CODE:
             * $check = @ftp_mkdir($conn_id, $destination_path);
             * 
             * REASON FOR CHANGE: $check is redundant
             */
            if (@ftp_mkdir($conn_id, $destination_path)) { # make new folder
                /**
                 * OLD CODE:
                 * $upload = @ftp_put($conn_id, $destination_file, $source_file, FTP_BINARY);
                 * 
                 * REASON FOR CHANGE: $upload is redundant
                 */
                if (@ftp_put($conn_id, $destination_file, $source_file, FTP_BINARY)) { # upload the file & check upload status
                    return true;
                } else {
                    return false;
                }
            } else {
                return false;
            }
        }

    }
    ftp_close($conn_id); # close ftp
}

版本 2:

$ftp_server = "HOST_NAME";
$ftp_user_name = "USERNAME";
$ftp_user_pass = "PASSWORD";
$ftp_folder = "/FTP_DIRECTORY";
$path = "/testing/";
$file_name = $_FILES["theFile"]["name"];
$source_file = $_FILES["theFile"]["tmp_name"];
$destination_path = $ftp_folder . $path;
$destination_file = $destination_path . $file_name;


function uploadFTP($ftp_server, $ftp_user_name, $ftp_user_pass, $source_file, $destination_file, $destination_path) {
    $conn_id = ftp_connect($ftp_server) or die('Couldn\'t connect to ' . $ftp_server); # set up basic connection
    $login_result = @ftp_login($conn_id, $ftp_user_name, $ftp_user_pass); # login with username and password

    if (empty($conn_id) || empty($login_result)) { # check connection 
        return false;
    } else {
        if (@ftp_chdir($conn_id, $destination_path)) { # check to see if folder is there
            /**
             * OLD CODE:
             * if (ftp_put($conn_id, $destination_file, $source_file, FTP_BINARY)) { # upload the file & check upload status
             *    return true;
             * } else {
             *    return false;
             * }
             * 
             * REASON FOR CHANGE: DRY (Don't Repeat Yourself). Abstracted code above to function.
             */
             return uploadFTP_ftpPut($conn_id, $destination_file, $source_file);
        } else {
            if (@ftp_mkdir($conn_id, $destination_path)) { # make new folder
                /**
                 * OLD CODE
                 * ...
                 * 
                 * REASON FOR CHANGE: See above.
                 */
                return uploadFTP_ftpPut($conn_id, $destination_file, $source_file);
            } else {
                return false;
            }
        }

    }
    ftp_close($conn_id); # close ftp
}

function uploadFTP_ftpPut($conn_id, $destination_file, $source_file){
    # upload the file & check upload status
    if (@ftp_put($conn_id, $destination_file, $source_file, FTP_BINARY)) return true;
    else return false;
}

没有评论的版本 3:

$ftp_server = "HOST_NAME";
$ftp_user_name = "USERNAME";
$ftp_user_pass = "PASSWORD";
$ftp_folder = "/FTP_DIRECTORY";
$path = "/testing/";
$file_name = $_FILES["theFile"]["name"];
$source_file = $_FILES["theFile"]["tmp_name"];
$destination_path = $ftp_folder . $path;
$destination_file = $destination_path . $file_name;


function uploadFTP($ftp_server, $ftp_user_name, $ftp_user_pass, $source_file, $destination_file, $destination_path) {
    $conn_id = ftp_connect($ftp_server) or die('Couldn\'t connect to ' . $ftp_server); # set up basic connection
    $login_result = @ftp_login($conn_id, $ftp_user_name, $ftp_user_pass); # login with username and password

    if (empty($conn_id) || empty($login_result)) return false; # check connection 

    if (@ftp_chdir($conn_id, $destination_path)) { # check to see if folder is there
         return uploadFTP_ftpPut($conn_id, $destination_file, $source_file);
    } else {
        if (@ftp_mkdir($conn_id, $destination_path)) { # make new folder
            return uploadFTP_ftpPut($conn_id, $destination_file, $source_file);
        } else {
            return false;
        }
    }

    # BTW - you never get here :) 
    ftp_close($conn_id); # close ftp
}

function uploadFTP_ftpPut($conn_id, $destination_file, $source_file){
    # upload the file & check upload status
    if (@ftp_put($conn_id, $destination_file, $source_file, FTP_BINARY)) return true;
    else return false;
}
于 2013-01-03T21:02:05.433 回答