ftps (FTPS是一種以擴展常用的文件傳輸協議(FTP)的添加了對傳輸層安全性(TLS)和安全套接字層(SSL)的加密協議的支持。)本身無法使用CI內建的lib去作,需要額外自己建一個libraries。
首先先在libraires中建立一個檔案
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 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 |
<?php /** * FTP with Implicit SSL/TLS Class * * Simple wrapper for cURL functions to transfer an ASCII file over FTP with implicit SSL/TLS * * @category Class * @author Max Rice * @since 1.0 */ class FtpsUpload { /** @var resource cURL resource handle */ private $curl_handle; /** @var string cURL URL for upload */ private $url; /** * Connect to FTP server over Implicit SSL/TLS * * * @access public * @since 1.0 * @param string $username * @param string $password * @param string $server * @param int $port * @param string $initial_path * @param bool $passive_mode * @throws Exception - blank username / password / port * @return \FTP_Implicit_SSL */ public function __construct( $ary_params ) { $username = "username"; $password = "password"; $server = "server"; $port = 990; $initial_path = ''; $passive_mode = true; // check for blank username if ( ! $username ) throw new Exception( 'FTP Username is blank.' ); // don't check for blank password (highly-questionable use case, but still) // check for blank server if ( ! $server ) throw new Exception( 'FTP Server is blank.' ); // check for blank port if ( ! $port ) throw new Exception ( 'FTP Port is blank.', WC_XML_Suite::$text_domain ); // set host/initial path $this->url = "ftps://{$server}/{$initial_path}"; // setup connection $this->curl_handle = curl_init(); // check for successful connection if ( ! $this->curl_handle ) throw new Exception( 'Could not initialize cURL.' ); // connection options $options = array( CURLOPT_USERPWD => $username . ':' . $password, CURLOPT_SSL_VERIFYPEER => false, // don't verify SSL CURLOPT_SSL_VERIFYHOST => false, CURLOPT_FTP_SSL => CURLFTPSSL_ALL, // require SSL For both control and data connections CURLOPT_FTPSSLAUTH => CURLFTPAUTH_DEFAULT, // let cURL choose the FTP authentication method (either SSL or TLS) CURLOPT_UPLOAD => true, CURLOPT_PORT => $port, CURLOPT_TIMEOUT => 60, ); // cURL FTP enables passive mode by default, so disable it by enabling the PORT command and allowing cURL to select the IP address for the data connection if ( ! $passive_mode ) $options[ CURLOPT_FTPPORT ] = '-'; // set connection options, use foreach so useful errors can be caught instead of a generic "cannot set options" error with curl_setopt_array() foreach ( $options as $option_name => $option_value ) { if ( ! curl_setopt( $this->curl_handle, $option_name, $option_value ) ) throw new Exception( sprintf( 'Could not set cURL option: %s', $option_name ) ); } $this->upload($ary_params['file_name'], $ary_params['file']); // print_r($ary_params);exit; } /** * Write file into temporary memory and upload stream to remote file * * @access public * @since 1.0 * @param string $file_name - remote file name to create * @param string $file - file content to upload * @throws Exception - Open remote file failure or write data failure */ public function upload( $file_name, $file ) { // set file name if ( ! curl_setopt( $this->curl_handle, CURLOPT_URL, $this->url . $file_name )) throw new Exception ( "Could not set cURL file name: $file_name" ); // open memory stream for writing $stream = fopen( 'php://temp', 'w+' ); // check for valid stream handle if ( ! $stream ) throw new Exception( 'Could not open php://temp for writing.' ); // write file into the temporary stream fwrite( $stream, $file ); // rewind the stream pointer rewind( $stream ); // set the file to be uploaded if ( ! curl_setopt( $this->curl_handle, CURLOPT_INFILE, $stream ) ) throw new Exception( "Could not load file $file_name" ); // upload file if ( ! curl_exec( $this->curl_handle ) ) throw new Exception( sprintf( 'Could not upload file. cURL Error: [%s] - %s', curl_errno( $this->curl_handle ), curl_error( $this->curl_handle ) ) ); // close the stream handle fclose( $stream ); } /** * Attempt to close cURL handle * Note - errors suppressed here as they are not useful * * @access public * @since 1.0 */ public function __destruct() { @curl_close( $this->curl_handle ); } } |
我把「file_name」跟「file」(內容)放在建構子中,也就是說一但呼叫了這個class,他就會立即執行上傳的動作;呼叫方式如下:
1 2 3 4 |
$ary_params['file_name'] = $file_name; $ary_params['file'] = file_get_contents($filename); $this->load->library('ftpsUpload', $ary_params ); |
ftps是透過curl的方式上傳,比較特別一點,需要注意一下。
參考來源:
HOW TO CONNECT TO AN FTP SERVER WITH IMPLICIT SSL/TLS USING PHP