ftp/ReadDirectory.phps

<?php
//////////////////////////////////////////////////////////
// ReadDirectory.php
//
// Purpose: Connects to the FTP server, changes the directory and outputs the content of the directory.
//
// Tested with: PHP 5.x+
//
// Technical support: support@smartftp.com
//
// Copyright (c) by SmartSoft Ltd.
//
// References: <a href="https://www.php.net/manual/en/ref.com.php">PHP and COM</a>
//////////////////////////////////////////////////////////


// helper

//////////////////////////////////////////////////////////
// DateTimeToUnix
//
// Purpose: Converts OLE DateTime format to Unix (seconds since 1.1.1970)
//
// Example: Input: '38644.8661226852'
function DateTimeToUnix($dt)
{
	$dt = explode( '.', $dt );
	
	// We get the date portion (as UNIX timestamp, which is "native"
	// PHP format).
	$tm = mktime( 0, 0, 0, 1, (1 + intval( $dt[0] ) - 25569), 1970 );
	$tm = date( 'Y-m-d', $tm );
	//echo 'Date: ' . $tm . "\n";
	
	// We'll need it later split to year, month and day parts.
	$tm = explode( '-', $tm );
	
	// We get the time portion recalculated to seconds of the day.
	$secs = intval( 60 * 60 * 24 * doubleval( '0.'.intval($dt[1]) ) );
	//echo 'Secs: ' . $secs . "\n";
	
	// We join all this to get date with time and get it as timestamp
	$tm = mktime( 0, 0, $secs, intval( $tm[1] ), intval( $tm[2] ), intval( $tm[0] ) );

	return $tm;
}

// main
try 
{
	// Create COM object.
	// Requires php_com_dotnet.dll extension to be enabled inside of php.ini
	$ftpConnection = new COM("sfFTPLib.FTPConnectionSTA");

	$ftpConnection->Host = "ftp.smartftp.com";
	$ftpConnection->Username = "anonymous";
	$ftpConnection->Password = "test@test.com";
//	$ftpConnection->Port = 21;
	$ftpConnection->Protocol = 4; // ftpProtocolPreferTLS
//	$ftpConnection->Passive = 1;

	$ftpConnection->Connect();
	print("Connect() successful.\n");
	$ftpConnection->ChangeDirectory("/");
	print("ChangeDirectory() successful.\n");

	$ftpDirectory = $ftpConnection->ReadDirectory();
	foreach ($ftpDirectory as $ftpItem) 
	{
		// Note: PHP doesn't support the FILETIME type (FileTime property)
		// OLE Date to seconds

		print("Type=" . $ftpItem->Type . "; Name=\"" . $ftpItem->Name . "\"; Size=" . $ftpItem->Size . "; Date=" . date("Y-m-d\\TH:i:s", DateTimeToUnix($ftpItem->ModifyTimeAsDate))."\n");
	}
} 
catch (com_exception $e) { 
  print("COM Exception: ". $e . "\n"); 
}