The general principle is to traverse all non-exclusive files in the project and then obtainThe file modification time is later than the last modification time of the file. And then upload these files to the corresponding directory through ftp. the specific code is as follows:The code is messy because it is just a tool. sorry.
- Error_reporting (7 );
- If ($ _ SERVER ['server _ ADDR ']) exit; // disable running under the web SERVER
- $ _ GET ['clude'] = array('number.txt ', 'uploads', 'zend', 'docs', 'cache', 'you', 'managesdk '); // exclude the upload directory and define it as a global variable
- $ Fileobj = new FilerFile ();
- $ Path = "/data/longtu/"; // 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); // link to the 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 a folder is used
- * 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 appendFilectime ($ 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 getFileByFilectime ($ 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 ['clude']) 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-> getFileByFilectime ($ abspath_file) continue;
- $ File = (rtrim ($ pathP ,"/")? Rtrim ($ pathP, "/"). "/": ""). $ f;
- $ Files [] = $ file;
- }
- }
- }
- Return $ files;
- }
- }
- /**
- * Copy the FTP class of CodeIgniter
- * Basic FTP operations:
- * 1) login; connect
- * 2) current directory file list; filelist
- * 3) Directory Change; chgdir
- * 4) rename/move; rename
- * 5) create a folder; mkdir
- * 6) delete; delete_dir/delete_file
- * 7) upload and upload
- * 8) 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 the 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 ID (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 ID (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 ID
- * @ Param string remote directory identification (ftp)
- * @ Param string: auto | ascii
- * @ Param int: List of uploaded file permissions
- * @ 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 ID
- * @ 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 ID
- * @ 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 an object
- *
- * @ Access public
- * @ Param string File ID (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 a folder
- *
- * @ Access public
- * @ Param string Directory ID (ftp)
- * @ Return boolean
- */
- Public function delete_dir ($ path ){
- If (! $ This-> _ isconn ()){
- Return FALSE;
- }
- // Add the backslash '\' to the '/' character of the directory macro '\'
- $ Path = preg_replace ("/(. + ?) \/* $/"," \ 1/", $ path );
- // Obtain the directory file list
- $ Filelist = $ this-> filelist ($ path );
- If ($ filelist! = False and count ($ filelist)> 0 ){
- Foreach ($ filelist as $ item ){
- // If the folder cannot be deleted, it may be a folder.
- // Recursively call delete_dir ()
- If (! @ Delete_file ($ item )){
- $ This-> delete_dir ($ item );
- }
- }
- }
- // Delete a 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 ID (ftp)
- * @ Return boolean
- */
- Public function chmod ($ path, $ perm ){
- If (! $ This-> _ isconn ()){
- Return FALSE;
- }
- // Only PHP5 defines the function for modifying permissions (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 ID (ftp)
- * @ Return array
- */
- Public function filelist ($ path = '.'){
- If (! $ This-> _ isconn ()){
- Return FALSE;
- }
- Return ftp_nlist ($ this-> conn_id, $ path );
- }
- /**
- * Disable 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 the 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 );
- }
- /**
- * Determine the 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 extension extensions from file names
- *
- * @ Access private
- * @ Param string Directory ID
- * @ Return string
- */
- Private function _ getext ($ filename ){
- If (FALSE === strpos ($ filename ,'.')){
- Return 'txt ';
- }
- $ Extarr = explode ('.', $ filename );
- Return end ($ extarr );
- }
- /**
- * Extended definition of FTP transmission mode ascii or binary from the suffix
- *
- * @ Access private
- * @ Param string suffix extension
- * @ Return string
- */
- Private function _ settype ($ ext ){
- $ Text_type = array (
- 'Txt ',
- 'Text ',
- 'Php ',
- 'Phps ',
- 'Php4 ',
- 'Js ',
- 'Css ',
- 'Htm ',
- 'Html ',
- 'Phpml ',
- 'Shtml ',
- 'Log ',
- 'Xml'
- );
- Return (in_array ($ ext, $ text_type ))? 'Ascii ': 'binary ';
- }
- /**
- * Error log records
- *
- * @ 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 */
|