PHP to upload files via ftp _php tips

Source: Internet
Author: User
Tags chmod ftp connection ftp login mkdir rtrim file permissions ftp transfer zend

Approximate principle

Iterate through all the non-excluded files in the project, and then get files that were modified later than the last time the file was modified

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 messy, forgive me

<?php error_reporting (7); if ($_server[' server_addr ']) exit;//prohibits running $_get[' exclude ' on the Web server = 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 modification 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 = new FTP ();       $ftp->connect ($config);
Link 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)) determines whether the folder * Enter description ... * @author Administrator */class Filerfile {VA
 R $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, "<?php 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");
  The 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;
  The 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->getfilebyfilectime ($abspath _file)) continue; $file = (RTrim ($pathP, "/") RTrim ($pathP, "/"). "
     /":" "). $f;
    $files [] = $file;
 }} return $files;     }/** * Copy write CodeIgniter FTP class * FTP basic operation: * 1) landing; Connect * 2 List of current directory files;   FileList * 3) directory changes;   Chgdir * 4) renaming/moving;    Rename * 5 Create folder;       mkdir * 6) deleted;       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: $config = array (' hostname ' => ', ' username ' => ', ' Password ' => ', ' Port ' =&G t; '
  ...);
  */Public Function __construct ($config = Array ()) {if (count ($config) > 0) {$this->_init ($config); }/** * FTP connection * @access public * @param array * @return Boolean/Public function connect ($conf IG = 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; /** * 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 identification (FTP) * @param int file Permissions List * @return Boolean/Pub
  Lic 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 permissions 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[". $localpat H. "]
   /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->_is
  Conn ()) {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[". $localp Ath. "]
   -remotepath[". $remotepath."] ");}
  return FALSE;
 return TRUE; /** * Rename/move * * @access public * @param string remote directory Identification (FTP) * @param string new directory ID * @param boolean judgment is a felony. Name (FALSE) or Move (TRUE) * @return Boolean/Public Function rename ($oldname, $newname, $move = FALSE) {if (! $this-&G
  T;_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 identification (FTP) * @return Boolean/Public Function Delete_dir (
  $path) {if (! $this->_isconn ()) {return FALSE; Add a backslash ' \ ' $path = Preg_replace ("/(. +?) for the '/' character of the directory macro.
  \/*$/"," \\1/", $path);
  Gets the directory file list $filelist = $this->filelist ($path); if ($filelist!== FALSE and Count ($filelist) > 0) {foreach ($filelist as $item) {//If we cannot delete it, then it may be a folder//So
    We recursively 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 identification (FTP) * @return Boolean/Public function chmod ($pat
  H, $perm) {if (! $this->_isconn ()) {return FALSE; ///The function that modifies permissions is defined in PHP5 (FTP) if (! function_exists (' Ftp_chmod ')) {if ($this->debug = = TRUE) {$this->_er
   Ror ("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); /** * Shut down 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 arrays * @return void/Private Function _init ($con
   Fig = Array ()) {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-&
 gt;conn_id, $this->username, $this->password); /** * Judge Con_id * * @access Private * @return Boolean/Private Function _isconn () {if (! Is_resource ($th
   is->conn_id)) {if ($this->debug = = TRUE) {$this->_error ("ftp_no_connection");
  return FALSE;
 return TRUE; /** * get suffix extension from filename * * @access private * @param string directory ID * @return string/Private function _getext ($ FileName) {if (F)Alse = = 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 Logging * @access Prvate * @return Boolean/Private Function _error ($msg) {return @file_put_cont Ents ('/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*/

The above is the entire contents of this article, I hope you can enjoy.

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.