Rough principle
Iterate through all non-excluded files in the project and get the files for 文件修改时间晚于文件上一次修改时间
Then upload these files to the corresponding directory via ftp
The specific code is as follows:
Because it’s just a tool, the code is very messy, sorry
- error_reporting(7);
- if ($_SERVER['SERVER_ADDR'])exit;//It is forbidden to run under the web server
- $_GET['exclude'] = array('number.txt ','uploads','Zend','docs','cache','You','managesdk'); //Exclude upload directory, defined as a global variable
- $fileobj = new FilerFile();
- $path = "/data/longtu/"; //The root directory of the project directory
- $files = $fileobj->Zip($path); //Filter out the latest modified files
- $path = str_replace("/data/longtu/ ","",$path);
- $config = array(
- 'hostname' => 'xxx.xxx.xx.xxx', //ftp server address
- 'username' => 'xxx', // ftp user
- 'password' => '?xxxxxxxxxxx', //ftp password
- 'port' => 21 //Port
- );
- $ftp = new Ftp();
- $ftp->connect($ config); //Linked server
- //$a=$ftp->filelist();
- $LOCAL_ROOT = realpath(dirname(__DIR__)."/../../");
- chdir($LOCAL_ROOT) ;
- foreach ($files as $k=>$v){
- $f = $path.$v;
- $tmp = $ftp->upload($f, $f);
- if($tmp) {
- echo "upload $f -> success n";
- }
- }
- //$ftp->download('ftp_upload.log','ftp_download.log');
- //
- //$ftp- >upload('ftp_err.log','ftp_upload.log');
- //$ftp->download('ftp_upload.log','ftp_download.log');
- /*
- *
- *
- * $ dir = "/test";
- if(@ftp_chdir($conn, $dir))
- Determine whether it is a folder
- * Enter description here ...
- * @author Administrator
- *
- */
- class FilerFile
- {
- var $time_path;
- private $fctimes = array();
- function Zip($dir)
- {
- $this->time_path = rtrim($dir,"/")."/.~~~time.php" ;
- //@unlink($this->time_path);
- $filelist = $this -> GetFileList($dir);
- file_put_contents($this->time_path,"fctimes,true).";");
- return $filelist;
- }
- function appendFiletime($file)
- {
- $time_file_path = $this->time_path;
- $ftime = @include( $time_file_path);
- $ftime = $ftime ? $ftime : array();
- $time = filectime($file);
- if(!file_exists($time_file_path))file_put_contents($time_file_path," }
- function getFileByFiletime($file)
- {
- static $time_data ;
- $time_file_path = $this->time_path;
- if (!$time_data){
- $time_data= @include_once($time_file_path);
- }
- $time_data = $time_data ? $time_data : array();
- //var_dump($file,$time_data[$file] == filectime($file));
- //echo $file."t".$time_data [$file]."n";
- if ($time_data[$file] == filemtime($file)){
- return false;
- }else{
- return $file;
- }
- }
- function GetFileList($dir ,$path="")
- {
- static $tmpp = "";
- if ($path=="" && !$tmpp){
- $tmpp = $dir;
- }
- $d = dir($dir) ;
- $files = array();
- if ($d)
- {
- $pathP=str_replace($tmpp,"",$dir);
- $pathP=str_replace(array("\\","/") ,DIRECTORY_SEPARATOR,$pathP);
- $pathP=str_replace(DIRECTORY_SEPARATOR.DIRECTORY_SEPARATOR,DIRECTORY_SEPARATOR,$pathP);
- while($f = $d->read())
- {
- if ($f == '.' || $f=='..' || $f{0}=='.' || $f=='Zend' || in_array($f, $_GET['exclude']))continue;
- $newdir = rtrim($dir,"/")."/".$f;
- if (is_dir($newdir)){
- $files = array_merge($files,$this->GetFileList($newdir,$ newdir));
- }else{
- $abspath_file = (rtrim($dir,"/") ? rtrim($dir,"/")."/" : "").$f;
- $this-> fctimes[$abspath_file] = filemtime($abspath_file);
- if (!$this->getFileByFiletime($abspath_file))continue;
- $file = (rtrim($pathP,"/") ? rtrim($pathP," /")."/" : "").$f;
- $files[] = $file;
- }
- }
- }
- return $files;
- }
- }
- /**
- * Imitate CodeIgniter's FTP class
- * 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 configuration array
- * @return boolean
- */
- public function connect($config = array()) {
- if(count($config) > 0) {
- $this->_init($config);
- }
- if(FALSE === ($this->conn_id = @ftp_connect($this->hostname,$this->port))) {
- if($this->debug === TRUE) {
- $this->_error("ftp_unable_to_connect");
- }
- return FALSE;
- }
- if( ! $this->_login()) {
- if($this->debug === TRUE) {
- $this->_error("ftp_unable_to_login");
- }
- return FALSE;
- }
- if($this->passive === TRUE) {
- ftp_pasv($this->conn_id, TRUE);
- }
- return TRUE;
- }
- /**
- * Whether the folder exists
- * @param unknown_type $path
- */
- public function is_dir_exists($path)
- {
- return $this->chgdir($path);
- }
- /**
- * Directory change
- *
- * @access public
- * @param string directory identification (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 identification (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 identifier
- * @param string remote directory identifier (ftp)
- * @param string upload mode auto || ascii
- * @param int file permissions after uploading List
- * @return boolean
- */
- public function upload($localpath, $remotepath, $mode = 'auto', $permissions = NULL) {
- if( ! $this->_isconn()) {
- return FALSE;
- }
- if( ! file_exists($localpath)) {
- if($this->debug === TRUE) {
- $this->_error("ftp_no_source_file:".$localpath);
- }
- return FALSE;
- }
- if($mode == 'auto') {
- $ext = $this->_getext($localpath);
- $mode = $this->_settype($ext);
- }
- $mode = ($mode == 'ascii') ? FTP_ASCII : FTP_BINARY;
- $result = @ftp_put($this->conn_id, $remotepath, $localpath, $mode);
- if($result === FALSE) {
- if($this->debug === TRUE) {
- $this->_error("ftp_unable_to_upload:localpath[".$localpath."]/remotepath[".$remotepath."]");
- }
- return FALSE;
- }
- if( ! is_null($permissions)) {
- $this->chmod($remotepath,(int)$permissions);
- }
- return TRUE;
- }
- /**
- * Download
- *
- * @access public
- * @param string remote directory identification (ftp)
- * @param string local directory identification
- * @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 identification (ftp)
- * @param string New directory identification
- * @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 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 identifier (ftp)
- * @return boolean
- */
- public function delete_dir($path) {
- if( ! $this->_isconn()) {
- return FALSE;
- }
- //对目录宏的'/'字符添加反斜杠''
- $path = preg_replace("/(.+?)/*$/", "\1/", $path);
- //获取目录文件列表
- $filelist = $this->filelist($path);
- if($filelist !== FALSE AND count($filelist) > 0) {
- foreach($filelist as $item) {
- //如果我们无法删除,那么就可能是一个文件夹
- //所以我们递归调用delete_dir()
- if( ! @delete_file($item)) {
- $this->delete_dir($item);
- }
- }
- }
- //删除文件夹(空文件夹)
- $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 identification (ftp)
- * @return boolean
- */
- public function chmod($path, $perm) {
- if( ! $this->_isconn()) {
- return FALSE;
- }
- //只有在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 directory file list
- *
- * @access public
- * @param string directory identification (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 configuration array
- * @return void
- */
- private function _init($config = array()) {
- foreach($config as $key => $val) {
- if(isset($this->$key)) {
- $this->$key = $val;
- }
- }
- //特殊字符过滤
- $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 identifier
- * @return string
- */
- private function _getext($filename) {
- if(FALSE === strpos($filename, '.')) {
- return 'txt';
- }
- $extarr = explode('.', $filename);
- return end($extarr);
- }
- /**
- * Define FTP transfer mode ascii or binary from suffix extension
- *
- * @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 prvate
- * @return boolean
- */
- private function _error($msg) {
- return @file_put_contents('/tmp/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 ftp.php*/
- /*Location /Apache Group/htdocs/ftp.php*/
复制代码
|