My goal is to connect to an FTP account, read the files in a particular folder, grab the content and list out to my screen.
This is what I have:
// 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;
}
}
The above works well to get the file names I need to grab the content from. Next I want to recurse through the filename array and store the content into a variable for further processing.
I am not sure how to do this, I would imagine it would need to be something like this though:
foreach ($files AS $file )
{
$handle = fopen($filename, "r");
$contents = fread($conn_id, filesize($file));
$content[$file] = $contents;
}
The above idea comes from here:
PHP: How do I read a .txt file from FTP server into a variable?
Although I don't like the idea of having to connect each time to grab the file contents, would prefer to do it on the initial instance.