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
//
// 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.
	$objFTPConnection = new COM("sfFTPLib.FTPConnectionSTA");
	
	$objFTPConnection->Host = "smartftp.com";
	$objFTPConnection->Username = "anonymous";
	$objFTPConnection->Password = "test@test.com";
	$objFTPConnection->Port = 21;
	$objFTPConnection->Protocol = 0; // ftpProtocolNormal
	$objFTPConnection->Passive = 1;
	$objFTPConnection->MLST = 1;
				
	$objFTPConnection->Connect();
	print("Connect() successful.\n");		
	$objFTPConnection->ChangeDirectory("/SmartFTP");
	print("ChangeDirectory() successful.\n");		
				
	$objFTPDirectory = $objFTPConnection->ReadDirectory();
	foreach ($objFTPDirectory as $objFTPItem) 
  {
  	// Note: PHP doesn't support the FILETIME type (FileTime property)
  	// OLE Date to seconds
  				
  	print("Type=".$objFTPItem->Type."; Name=\"".$objFTPItem->Name."\"; Size=".$objFTPItem->Size."; Date=".date("Y-m-d\\TH:i:s", DateTimeToUnix($objFTPItem->ModifyTimeAsDate))."\n");
	}
} 
catch (com_exception $e) { 
  print("COM Exception: ".$e . "\n"); 
}