-1

我尝试了一些代码来使用 FTP 下载文件,但文件没有在计算机客户端中发送。此代码只是从服务器计算机的另一个文件夹中复制文件。你能告诉我解决方案吗?这是我的代码:

<?php
//FTP File Download
$file_name      = 'myfile.xml';
$destination    = '/download/'.$file_name;
$ftp_user_name  = 'chelsea';
$ftp_user_pass  = 'drogba';
$ftp_server = '192.168.1.1';
$conn_id    = ftp_connect($ftp_server);
$login_result   = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);

ftp_nb_get($conn_id, $destination, $file_name, FTP_BINARY);
ftp_close($conn_id);
?>
4

3 回答 3

0

确保 Ftp 服务器允许获取文件服务器到服务器,如果不允许,则不能使用此功能。

并测试这段代码

<?php

// define some variables
$local_file = '<save-file-as>';
$server_file = '<server-file-name>';
$server_directory='/';
$ftp_user_name='<ftp-user>';
$ftp_user_pass='<ftp-pass>';
$ftp_server='<ftp-host-name>';
// set up basic connection
$conn_id = ftp_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, $server_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);

?>
于 2013-01-23T08:16:17.773 回答
0

脚本将文件从第二台服务器下载到运行脚本的服务器后,您可以将其发送到客户端,并附上readfile相应的标头(请参阅此处:用于下载文件 php的标头)

当然,它会通过 HTTP 发送,因为对 PHP 脚本的请求是 HTTP 请求,您不能即时更改协议。

于 2013-01-23T08:48:48.823 回答
0

尝试使用 curl 从 ftp 下载文件

$curl = curl_init();
$file = fopen("myfile.xml", 'w');
curl_setopt($curl, CURLOPT_URL, "ftp://ftp.yourdomain.com/myfile.xml"); #input
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_FILE, $file); #output
curl_setopt($curl, CURLOPT_USERPWD, "$_FTP[username]:$_FTP[password]");
curl_exec($curl);
curl_close($curl);
fclose($file);

PHP:从 ftp 服务器下载任何文件到硬盘?

于 2013-01-23T08:21:31.850 回答