1

I try to read the dir and wanna get the last files from the current date.

When I put the url in the browser I got a result of all the files, which are in the ftp-directory.

So I have the proof, that the ftp-connectionparameter still works.

enter image description here

When I try to start the following function, then I get the error

RecursiveDirectoryIterator::__construct(ftp://...@example.com:4242): failed to open dir: operation failed

Here is the Exception from symfony:

enter image description here

/**
 * @Route("/download", name="getfile")
 */
public function getFileWithFtp()
{

    $host = "example.com";
    $username = "username";
    $userpass = "userpass";
    $port = 4242;


    $url = 'ftp://' . $username . ':' . $userpass . '@' . $host . ':' . $port .'/';
    $datum = date('Y-m-d');

    $finder = new Finder();
    $iterator = $finder
        ->files()
        ->in($url)
        ->name('*BEHWN.TXT')
        ->date($datum);

    $anzahl = count($iterator);

    return $this->render('ftp/index.html.twig', [
        'controller_name' => 'FtpController',
        'url' => $url,
        'anzahl' => $anzahl
    ]);
}

WHen i open the url with the file with file_gets_content($url."filename.txt"), then i get the content without error.

Only it seems that I dont use the Finder from Symfony not correct.

My current Symfony is 4.1.4 and I had cleared the caches and also i deleted the cache-files manually.

Thanks for every tipp

Here is the link to the symfony-finder-component: https://symfony.com/doc/current/components/finder.html

As the Finder uses PHP iterators, you can pass any URL with a supported protocol:

Here is the part of the FTP code from the documentation:

// always add a trailing slash when looking for in the FTP root dir
$finder->in('ftp://example.com/');

// you can also look for in a FTP directory
$finder->in('ftp://example.com/pub/');

2 Answers2

0

As stated in the docs, what you have in $iterator is a PHP iterator, so you need to use iterator_count:

$anzahl =  iterator_count($iterator);
MazzCris
  • 1,812
  • 1
  • 14
  • 20
  • It would not work, I got the same error: RecursiveDirectoryIterator::__construct(ftp://...@example.com:4242): failed to open dir: –  Sep 12 '18 at 08:51
0

This WILL not work anymore IF you are using symfony v3.4.7 and above..

There is the issue: https://github.com/symfony/symfony/issues/27423

I did a fix but it will take a while till it gets merged i think https://github.com/symfony/symfony/pull/28604

Summary:

"In Finder[...]was released which should remove duplicate slashes from path names. However, this results in an error when used to find files in an FTP root dir."

"As a result, a working code before v3.4.7 result in breaking code after v3.4.7"

This is the method messing things up, including my fix https://github.com/symfony/symfony/blob/0670c48477b3d88787b6fe0dec168c5c8ae49c66/src/Symfony/Component/Finder/Finder.php#L741

DerDu
  • 688
  • 1
  • 13
  • 25