ftp/ReadDirectory.phps
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 | <?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" ); } |