使用php从ftp下载文件

时间:2009-08-24 19:23:36

标签: php ftp

我正在尝试创建一个应用程序,管理员可以在其中查看特定文件夹(预定义)中的文件,并且应该能够逐个下载它们。这些文件将存储在服务器上。有人可以指导我怎么做吗?

3 个答案:

答案 0 :(得分:1)

您是否尝试获取远程FTP服务器中的文件列表?如果是这样,那么你需要ftp extension或者使用PEAR的Net-FTP2扩展(它遗憾地被遗弃了,但你可以从那里挽救一些代码)

至于将来自FTP服务器的特定文件提供给远程用户,我建议readfile function

readfile('ftp://server/file.txt');

请记住,您还需要添加适当的标头,例如Content-Type,Content-Length等,readfile手册页有一些示例。

答案 1 :(得分:0)

我知道这是一个古老的问题,但我正在寻找一个类似的解决方案,我认为这将是一个有用的答案,它是有效的。 我希望它会帮助别人!!

    <?php

//ftp-server
$connection_id = ftp_connect("here put the link of FTP server");
$ftp_username = "here the username";
$ftp_password = "here the password";

$file_path_my_pc = "here the path that you want to save your file..";
$file_path_ftp_server = "here the path of the file in FTP server/the name of file.csv";




$login = ftp_login($connection_id, $ftp_username, $ftp_password);

if (!$login) {
    echo "Connection to ftp server has failed!! ";
    exit;
} else {
    echo "Connected has be done!!";
}

ftp_pasv($connection_id, true);

if (ftp_get($connection_id, $file_path_my_pc.'/here the name of your file in the FTP server', $file_path_ftp_server, FTP_ASCII)) {

    echo "File has been downloaded!!";
    ftp_close($connection_id);
    return true;

} else {
    ftp_close($connection_id);
    echo "fail ... ";
    echo "Connected has be stopped!!";
    return false;

}

答案 2 :(得分:-1)

检查file_get_contents功能。它可以满足您的需求。

例如:

<?php $file_content = file_get_contents ('ftp://mysite.com/file.txt'); ?>

然后您只需要使用file_put_contentsfwrite(根据您的喜好)将内容写入文件系统。