使用卷曲和PHP的FTP下载文件(Download a file from an FTP using

2019-10-16 16:21发布

我试图使用卷曲的FTP服务器下载文件。 我可以将URL粘贴到我的浏览器,下载的罚款。 问题是,有卷曲和PHP,我相信FTP服务器做一个重定向和卷曲不会跟随。 我收到错误消息,“服务器拒绝了您切换到指定目录”。

编辑:我曾在代码%2F和删除。 这是从以前的测试。

我的代码是:

$curl = curl_init();
$file = fopen("temp.zip", 'w');
curl_setopt($curl, CURLOPT_URL, "ftp://idx.living.net/idx_fl_ftp_down/idx_naples_dn/naples_data.zip");
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_FILE, $file);
curl_setopt($curl, CURLOPT_FTPSSLAUTH, CURLFTPAUTH_SSL);
curl_setopt($curl, CURLOPT_USERPWD, "myusername:mypassword");
curl_exec($curl);
echo curl_error($curl);

任何办法迫使卷曲表现在下载文件时,浏览器的方式做?

Answer 1:

我知道这不回答你的问题的方式应该是(使用curl)。 但这里有一个非卷曲代码下载到PHP的FTP服务器上的文件。

$local_file = 'output.rar';
$server_file = '/somedir/1/bar/foo/somearchive.rar';
$ftp_user_name='some_login';
$ftp_user_pass='Password';
$ftp_server='HOST';
// set up basic connection
$conn_id = ftp_ssl_connect($ftp_server);

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

/* uncomment if you need to change directories
if (ftp_chdir($conn_id, "<directory>")) {
    echo "Current directory is now: " . ftp_pwd($conn_id) . "\n";
} else { 
    echo "Couldn't change directory\n";
}
*/

// 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";
}

// close the connection
ftp_close($conn_id); 

笔记:

  1. ftp_ssl_connect()不能用于与SFTP使用。 用PHP使用SFTP,请参阅ssh2_sftp()。
  2. FTP服务器地址参数不应该有任何尾随的斜杠,不应与FTP://为前缀。


文章来源: Download a file from an FTP using Curl with PHP