File 1:
<?php /** * FTP class imitating CodeIgniter * Basic FTP operations: * 1) Log in; connect * 2) Current directory file list; filelist * 3) Directory change; chgdir * 4) Rename/move; rename * 5) Create folder; mkdir * 6) Delete; delete_dir/delete_file * 7) Upload; upload * 8) Download download * * @author quanshuidingdang */ class Ftp { private $hostname = ''; private $username = ''; private $password = ''; private $port = 21; private $passive = TRUE; private $debug = TRUE; private $conn_id = FALSE; /** * Constructor * * @param array Configuration array: $config = array('hostname'=>'','username'=>'','password'=>'','port'=>''...); */ public function __construct($config = array()) { if(count($config) > 0) { $this->_init($config); } } /** * FTP connection * * @access public * @param array Configure array * @return boolean */ public function connect($config = array()) { if(count($config) > 0) { $this->_init($config); } //Judge whether the ftp connection is openedif(FALSE === ($this->conn_id = @ftp_connect($this->hostname,$this->port))) { if($this->debug === TRUE) { $this->_error("ftp_unable_to_connect"); } return FALSE; } //Judge whether the login is successfulif( ! $this->_login()) { if($this->debug === TRUE) { $this->_error("ftp_unable_to_login"); } return FALSE; } //Judge whether to enable FTP passive modeif($this->passive === TRUE) { ftp_pasv($this->conn_id, TRUE); } return TRUE; } /** * Directory changes * * @access public * @param string directory identity (ftp) * @param boolean * @return boolean */ public function chgdir($path = '', $supress_debug = FALSE) { if($path == '' OR ! $this->_isconn()) { return FALSE; } $result = @ftp_chdir($this->conn_id, $path); if($result === FALSE) { if($this->debug === TRUE AND $supress_debug == FALSE) { $this->_error("ftp_unable_to_chgdir:dir[".$path."]"); } return FALSE; } return TRUE; } /** * Directory generation * * @access public * @param string directory identity (ftp) * @param int File permission list * @return boolean */ public function mkdir($path = '', $permissions = NULL) { if($path == '' OR ! $this->_isconn()) { return FALSE; } $result = @ftp_mkdir($this->conn_id, $path); if($result === FALSE) { if($this->debug === TRUE) { $this->_error("ftp_unable_to_mkdir:dir[".$path."]"); } return FALSE; } if( ! is_null($permissions)) { $this->chmod($path,(int)$permissions); } return TRUE; } /** * Upload * * @access public * @param string Local directory identity * @param string Remote directory identity (ftp) * @param string Upload mode auto || ascii * @param int File permission list after upload * @return boolean */ public function upload($localpath, $remotepath, $mode = 'auto', $permissions = NULL) { if( ! $this->_isconn()) { return FALSE; } //Judge whether the local file existsif( ! file_exists($localpath)) { if($this->debug === TRUE) { $this->_error("ftp_no_source_file:".$localpath); } return FALSE; } //Judge upload modeif($mode == 'auto') { //Get file suffix type$ext = $this->_getext($localpath); //Depend whether the upload mode is FTP_ASCII (text mode) or FTP_BINARY (binary mode);$mode = $this->_settype($ext); } $mode = ($mode == 'ascii') ? FTP_ASCII : FTP_BINARY; //Upload$result = @ftp_put($this->conn_id, $remotepath, $localpath, $mode); //Judge whether the upload is successfulif($result === FALSE) { if($this->debug === TRUE) { $this->_error("ftp_unable_to_upload:localpath[".$localpath."]/remotepath[".$remotepath."]"); } return FALSE; } //Discern whether the file permission is requiredif( ! is_null($permissions)) { $this->chmod($remotepath,(int)$permissions); } return TRUE; } /** * download * * @access public * @param string Remote directory identity (ftp) * @param string Local directory identity * @param string Download mode auto || ascii * @return boolean */ public function download($remotepath, $localpath, $mode = 'auto') { if( ! $this->_isconn()) { return FALSE; } if($mode == 'auto') { $ext = $this->_getext($remotepath); $mode = $this->_settype($ext); } $mode = ($mode == 'ascii') ? FTP_ASCII : FTP_BINARY; $result = @ftp_get($this->conn_id, $localpath, $remotepath, $mode); if($result === FALSE) { if($this->debug === TRUE) { $this->_error("ftp_unable_to_download:localpath[".$localpath."]-remotepath[".$remotepath."]"); } return FALSE; } return TRUE; } /** * Rename/move * * @access public * @param string Remote directory identity (ftp) * @param string New directory identity * @param boolean Determine whether to rename (FALSE) or move (TRUE) * @return boolean */ public function rename($oldname, $newname, $move = FALSE) { if( ! $this->_isconn()) { return FALSE; } $result = @ftp_rename($this->conn_id, $oldname, $newname); if($result === FALSE) { if($this->debug === TRUE) { $msg = ($move == FALSE) ? "ftp_unable_to_rename" : "ftp_unable_to_move"; $this->_error($msg); } return FALSE; } return TRUE; } /** * Delete the file * * @access public * @param string file identifier (ftp) * @return boolean */ public function delete_file($file) { if( ! $this->_isconn()) { return FALSE; } $result = @ftp_delete($this->conn_id, $file); if($result === FALSE) { if($this->debug === TRUE) { $this->_error("ftp_unable_to_delete_file:file[".$file."]"); } return FALSE; } return TRUE; } /** * Delete folder * * @access public * @param string directory identity (ftp) * @return boolean */ public function delete_dir($path) { if( ! $this->_isconn()) { return FALSE; } //Add a backslash to the '/' character of the directory macro$path = preg_replace("/(.+?)\/*$/", "\\1/", $path); //Get the directory file list $filelist = $this->filelist($path); if($filelist !== FALSE AND count($filelist) > 0) { foreach($filelist as $item) { //If we cannot delete it, it may be a folder //So we call delete_dir() recursively if( ! @delete_file($item)) { $this->delete_dir($item); } } } //Delete folder (empty folder) $result = @ftp_rmdir($this->conn_id, $path); if($result === FALSE) { if($this->debug === TRUE) { $this->_error("ftp_unable_to_delete_dir:dir[".$path."]"); } return FALSE; } return TRUE; } /** * Modify file permissions * * @access public * @param string directory identity (ftp) * @return boolean */ public function chmod($path, $perm) { if( ! $this->_isconn()) { return FALSE; } //The function that modifies permissions is defined only in PHP5 (ftp)if( ! function_exists('ftp_chmod')) { if($this->debug === TRUE) { $this->_error("ftp_unable_to_chmod(function)"); } return FALSE; } $result = @ftp_chmod($this->conn_id, $perm, $path); if($result === FALSE) { if($this->debug === TRUE) { $this->_error("ftp_unable_to_chmod:path[".$path."]-chmod[".$perm."]"); } return FALSE; } return TRUE; } /** * Get the directory file list * * @access public * @param string directory identity (ftp) * @return array */ public function filelist($path = '.') { if( ! $this->_isconn()) { return FALSE; } return ftp_nlist($this->conn_id, $path); } /** * Close FTP * * @access public * @return boolean */ public function close() { if( ! $this->_isconn()) { return FALSE; } return @ftp_close($this->conn_id); } /** * FTP member variable initialization * * @access private * @param array Configure array * @return void */ private function _init($config = array()) { foreach($config as $key => $val) { if(isset($this->$key)) { $this->$key = $val; } } //Special character filtering$this->hostname = preg_replace('|.+?://|','',$this->hostname); } /** * FTP login * * @access private * @return boolean */ private function _login() { return @ftp_login($this->conn_id, $this->username, $this->password); } /** * Judge con_id * * @access private * @return boolean */ private function _isconn() { if( ! is_resource($this->conn_id)) { if($this->debug === TRUE) { $this->_error("ftp_no_connection"); } return FALSE; } return TRUE; } /** * Get the suffix extension from the file name * * @access private * @param string directory identification * @return string */ private function _getext($filename) { if(FALSE === strpos($filename, '.')) { return 'txt'; } $extarr = explode('.', $filename); return end($extarr); } /** * Define FTP transfer mode from suffix extension ascii or binary * * @access private * @param string suffix extension * @return string */ private function _settype($ext) { $text_type = array ( 'txt', 'text', 'php', 'phps', 'php4', 'js', 'css', 'htm', 'html', 'phtml', 'shtml', 'log', 'xml' ); return (in_array($ext, $text_type)) ? 'ascii' : 'binary'; } /** * Error logging * * @access purchase * @return boolean */ private function _error($msg) { return @file_put_contents('ftp_err.log', "date[".date("Y-m-d H:i:s")."]-hostname[".$this->hostname."]-username[".$this->username."]-password[".$this->password."]-msg[".$msg."]\n", FILE_APPEND); } } /*End of file */ /*Location /Apache Group/htdocs/*/
File 2: ftp_demo.php
<?php require_once(''); $config = array( 'hostname' => '101.64.183.92', //Server address'username' => 'ftpadminuser', //FTP login account'password' => 'admin_user', //FTP login password'port' => 2112 //Port number); $ftp = new Ftp(); //connect$ftp->connect($config); //The first parameter upload is the local file name, and the second parameter is the FTP file name$rs=$ftp->upload('',''); if($rs){ echo 1; } // $ftp->download('ftp_upload.log','ftp_download.log');
The above is all the simple implementation methods for uploading files and videos with FTP classes that the editor brings to you. I hope it will be helpful to everyone and support me more~