我们收到此错误:
警告:ftp_get():无法打开到端口51461的数据连接:连接在第39行的/home/USER/public_html/SCRIPT/fetch_scripts/noose.php中超时
第39行:
ftp_get($CONNECTION_ID, '../ftp-in' . $SAVE_DIRECTORY . "/" . $SAVE_FILE_AS, $FTP_REMOTE_FILENAME, FTP_ASCII);
但是初始ftp设置如下,有没有办法将端口映射到此站点所需的21?
<?
ini_set("display_errors", "1");
error_reporting(-1);
//-------SYSTEM COMMANDS-------//
set_time_limit(0);
$start_time = time();
//-------CONFIGURE START-------//
$remote_server = 'ftp.SITE.com';
$remote_username = 'SITEFEED';
$remote_password = 'PASSWORD';
$remote_file = 'DATA.csv';
$local_save_path = '/DIRECTORY';
$local_save_filename = 'FILE.txt';
//-------CONFIGURE END-------//
//-------EXECUTION CODE-------//
fetch($remote_server,$remote_username,$remote_password ,$remote_file,$local_save_path, $local_save_filename);
//-------FUNCTIONS-------//
function fetch($FTP_SERVER,$FTP_USERNAME,$FTP_PASSWORD,$FTP_REMOTE_FILENAME,$SAVE_DIRECTORY,$SAVE_FILE_AS){
//MAKE CONNECTION...
$connected = 1;
$CONNECTION_ID = ftp_connect($FTP_SERVER);
if(!($CONNECTION_ID)){
echo "<font color = 'red'>Could not connect to FTP_SERVER: " . $FTP_SERVER . ".</font><br>";
$connected = 0;
}
$login_result = ftp_login($CONNECTION_ID,$FTP_USERNAME,$FTP_PASSWORD);
if((!$login_result)){
echo "<font color = 'red'>Check FTP_USERNAME & FTP_PASSWORD for " . $FTP_SERVER . ".</font><br>";
$connected = 0;
}
//IF CONNECTED...
if($connected == 1){
if(!is_dir('../ftp-in' . $SAVE_DIRECTORY)) { mkdir('../ftp-in' . $SAVE_DIRECTORY); }
ftp_get($CONNECTION_ID, '../ftp-in' . $SAVE_DIRECTORY . "/" . $SAVE_FILE_AS, $FTP_REMOTE_FILENAME, FTP_ASCII);
}
}
echo "<table><tr><Td><b>PAL FTP File Downloaded:</b></td><td>ftp-in/DATA/FILE.txt</td><td><i>Total Time: " . ( time() - $start_time) . " seconds.</i></td></tr>";
$start_time = time();
include("../db_import_scripts/pal_db_import.php");
?>
答案 0 :(得分:1)
正如Barmar所指出的,生成错误的端口号是用于FTP传输的本地/临时端口。当您在端口21上连接到远程FTP服务器时,在本地为您提供一个临时端口,用于TCP连接,在您的情况下为51461.这表示FTP服务器可能无法连接回您。
如果您确定要连接的FTP服务器有效/活动/能够接受连接,请尝试使用以下代码建立连接:
<?php
$local_file = 'local.zip';
$server_file = 'server.zip';
// setup connection
$conn_id = ftp_connect($ftp_server);
// login
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);
// try to download $server_file and save to $local_file
if (ftp_get($conn_id, $local_file, $server_file, FTP_BINARY))
{
echo "Successfully written to $local_file\n";
}
else
{
echo "There was a problem\n";
}
ftp_close($conn_id);
?>
如果这不起作用,您可能需要验证状态防火墙是否阻止本地端口接受从FTP服务器返回的连接。尝试禁用所有防火墙,然后重试。
此外,关于您关于映射到端口21&#39;的问题。如果您建立FTP连接但未指定端口,则假定您将使用端口21,因此不需要显式映射。
来源: