I
I
IVAN ***2021-02-09 12:20:38
PHP
IVAN ***, 2021-02-09 12:20:38

How to download only *.txt files from FTP server?

There is a program for analyzing *.txt files, you need to add it so that it can only *.txt from FTP from a separate folder, because there are other file extensions in that directory, but I don’t need them so that they don’t litter the disk on the virtual machine. What I managed to tweet, I apply, I ask the help of the masters in this craft :)

<?php
$host = "ftp.com";
$connect = ftp_connect($host);
if(!$connect) {
    echo("Ошибка соединения \n");
    exit;
}
else {
    echo("Соединение установлено \n");
}
$user = "user";
$password = "password";
$result = ftp_login($connect, $user, $password);
var_dump($result);
if(!$result) {
    echo("Ошибка авторизации \n");
    exit;
}
else {
    echo("Авторизация успешна \n");
}
    //ftp_chdir($connect, "/Ranorex/MyTest_insta_/result/");
$localfilename = "D:/xampp/htdocs/test/";
$serverfilename = "/Ranorex/MyTest_insta_/result/";
$listDir = ftp_nlist($connect, $serverfilename);
foreach ($listDir as $file) {
    $file = explode("/", $file);
    $file = array_pop($file);
    if(ftp_get($connect, $localfilename.$file, $serverfilename.$file, FTP_BINARY)){
        echo "Произведена запись в $localfilename\n";
    }
    else {
        echo "Не удалось завершить операцию\n";
    }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
Nadim Zakirov, 2021-02-09
@mingumou

Use regular expressions to check if the filename contains .txt at the end or not.

$text = 'имя файла.txt';
if (preg_match("/^.*\.txt$/", $text)) {
  echo 'yes';
} else {
  echo 'no';
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question