php取ftp文件,php – 使用一个连接读取FTP目录中每个文件的内容

我的目标是连接到FTP帐户,读取特定文件夹中的文件,抓取内容并列出到我的屏幕.

这就是我所拥有的:

// set up basic connection

$conn_id = ftp_connect('HOST_ADDRESS');

// login with username and password

$login_result = ftp_login($conn_id, 'USERNAME', 'PASSWORD');

if (!$login_result)

{

exit();

}

// get contents of the current directory

$contents = ftp_nlist($conn_id, "DirectoryName");

$files = [];

foreach ($contents AS $content)

{

$ignoreArray = ['.','..'];

if ( ! in_array( $content , $ignoreArray) )

{

$files[] = $content;

}

}

上面的工作很好,以获取我需要从中获取内容的文件名.接下来,我想通过文件名数组进行递归,并将内容存储到变量中以便进一步处理.

我不知道如何做到这一点,我想它会需要像这样:

foreach ($files AS $file )

{

$handle = fopen($filename, "r");

$contents = fread($conn_id, filesize($file));

$content[$file] = $contents;

}

虽然我不喜欢每次都要连接以获取文件内容的想法,但是我更喜欢在初始实例上进行连接.

解决方法:

为避免必须为每个文件连接/登录,请使用ftp_get并重用您的连接ID($conn_id):

foreach ($files as $file)

{

// Full path to a remote file

$remote_path = "DirectoryName/$file";

// Path to a temporary local copy of the remote file

$temp_path = tempnam(sys_get_temp_dir(), "ftp");

// Temporarily download the file

ftp_get($conn_id, $temp_path, $remote_path, FTP_BINARY);

// Read the contents of temporary copy

$contents = file_get_contents($temp_path);

$content[$file] = $contents;

// Discard the temporary copy

unlink($temp_path);

}

(您应该添加一些错误检查.)

标签:php,ftp

来源: https://codeday.me/bug/20190708/1401558.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
读取FTP远程文件夹下多个zip文件内容,可以使用JavaFTP客户端库来连接FTP服务器并获文件。一种解决方案是使用Apache Commons Net库,该库提供了FTP客户端API,使得连接FTP服务器和获文件变得非常简单。 以下是一个示例代码,可以连接FTP服务器,列出文件的所有zip文件,并读取每个zip文件内容: ```java import org.apache.commons.net.ftp.*; import java.io.*; public class FtpZipReader { public static void main(String[] args) throws IOException { // 创建FTP客户端 FTPClient ftpClient = new FTPClient(); // 连接FTP服务器 ftpClient.connect("ftp.example.com"); // 登录FTP服务器 ftpClient.login("username", "password"); // 设置文件类型为二进制文件 ftpClient.setFileType(FTP.BINARY_FILE_TYPE); // 改变工作目录到需要读取文件ftpClient.changeWorkingDirectory("/path/to/folder"); // 列出文件的所有zip文件 FTPFile[] files = ftpClient.listFiles("*.zip"); for (FTPFile file : files) { // 打开zip文件流 InputStream inputStream = ftpClient.retrieveFileStream(file.getName()); try (ZipInputStream zipInputStream = new ZipInputStream(inputStream)) { // 遍历zip文件的所有条目 ZipEntry zipEntry; while ((zipEntry = zipInputStream.getNextEntry()) != null) { // 读取zip文件的条目内容 ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); byte[] buffer = new byte[1024]; int length; while ((length = zipInputStream.read(buffer)) > 0) { outputStream.write(buffer, 0, length); } // 打印zip文件的条目内容 System.out.println(new String(outputStream.toByteArray())); } } } // 关闭FTP客户端连接 ftpClient.logout(); ftpClient.disconnect(); } } ``` 在此示例代码,我们使用FTPClient类的retrieveFileStream()方法来获zip文件的输入流,然后使用Java的ZipInputStream类来读取zip文件的条目内容读取完毕后,我们使用Java的String类将字节数组转换为字符串并打印出来。 请注意,此示例代码仅用于说明如何读取FTP远程文件夹下多个zip文件内容。实际情况,您需要根据实际需求进行修改和优化。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值