PHP implementation via FTP upload files, _php tutorial

Source: Internet
Author: User
Tags ftp connection ftp login rtrim ftp transfer

PHP implementation via FTP upload files,


Approximate principle

Traverse all non-excluded files in the project, and then get files that are modified later than the last modification time of the file

These files are then uploaded to the corresponding directory via FTP

The specific code is as follows:

Because it's just a tool, the code is messy, forgive me

<?phperror_reporting (7); if ($_server[' server_addr ') exit;//prohibit running under the Web server $_get[' Exclude '] = Array (' Number.txt ', ' Uploads ', ' Zend ', ' docs ', ' cache ', ' You ', ' managesdk '); Exclude upload directory, defined as global variable $fileobj = new Filerfile (); $path = "/data/longtu/"; The root directory of the project directory $files = $fileobj->zip ($path); Filter out the latest modified file $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 = NE       W 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 if folder * Enter description here ... * @author Administrator * */class filerfile{var $time _path; Private $FCT IMEs = 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, "<?php \ n return". Var_export ($this->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, "<?php \ n");  } 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[' Exclude '])) c    Ontinue; $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 CodeIgniter FTP class * FTP basic operation: * 1) login; Connect * 2) List of current directory files;   FileList * 3) directory changes;   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 arrays: $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 = Arra  Y ()) {if (count ($config) > 0) {$this->_init ($config); if (FALSE = = = ($this->conn_id = @ftp_connect ($this->hostname, $this->port))) {if ($this-&Gt;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; }/** * Folder exists * @param unknown_type $path */Public Function is_dir_exists ($path) {return $this->chgdir ($path); }/** * Directory changed * * @access public * @param string directory identifier (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_ch   Gdir: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 Identity * @param string remote directory ID (FTP) * @param string upload mode auto | | ASCII * @param int uploaded file permission 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 ID (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["). $localpat H. "] -remotepath[". $remotepath."] ");  return FALSE; } return TRUE; }/** * Rename/move * * @access public * @param string remote directory Identity (FTP) * @param string New directory identifier * @param boolean judgment is 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 identification (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; }//Add backslash ' \ ' to the '/' character of the directory macro $path = Preg_replace ("/(. +?)  \/*$/"," \\1/", $path);  Get list of directory files $filelist = $this->filelist ($path); if ($filelist!== FALSE and Count ($filelist) > 0) {foreach ($filelist as $item) {//If we cannot delete, then it may be a folder//so we hand    Call Delete_dir () 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 identifier (FTP) * @return Boolean */Public function chmod ($path, $perm  {if (! $this->_isconn ()) {return FALSE; The function (FTP) if (! function_exists (' Ftp_chmod ')) {if ($this->debug = = = TRUE) {if}////Only in PHP5 is defined, {$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 ID (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 ()) {retur  n FALSE; } return @ftp_close ($this->conn_id); }/** * FTP member variable initialization * * @access private * @param array configuration arrays * @return void */Private Function _init ($config = AR   Ray ()) {foreach ($config as $key + $val) {if (Isset ($this-$key)) {$this, $key = $val; }}//special character filter $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->c   ONN_ID) {if ($this->debug = = = TRUE) {$this->_error ("ftp_no_connection");  } return FALSE; } return TRUE; /** * Get suffix extension from 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);  }/** * Defines 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 log record * * @access prvate * @return Boolean */Private Function _error ($msg) {return @file_put_contents ('/t Mp/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*/

The above mentioned is the whole content of this article, I hope you can like.

http://www.bkjia.com/PHPjc/1019461.html www.bkjia.com true http://www.bkjia.com/PHPjc/1019461.html techarticle PHP Implementation via FTP upload files, presumably the principle of traversing all non-excluded files in the project, and then get the file modification time later than the file last modified time file and then the text ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.