PHP implementation upload files via FTP

Source: Internet
Author: User
Tags foreach file upload ftp ftp connection ftp login file permissions ftp transfer zend

In PHP, we can use the ftp_connect correlation function to achieve file upload and download function, in fact, the FTP client is the same operation, I would like to introduce you to the use of PHP to achieve

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

?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30-31 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 The 96 97 98 99 100 101 102 103 104 105 106 107 108 109 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140-1 41 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170-171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201-2 02 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231-232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262-2 63 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298-299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329-3 30 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359-360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390-3 91 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420-421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451-4 52 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481-482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 <?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 ' => '? xxxxxxx xxxx ',//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) to determine if the 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, "<?php N Return". Var_export ($this->fctimes,true); return $filelist; The function Appendfilectime ($file) {$time _file_path = $this->time_path; $ftime = @include ($time _file_path); $ftime = $f Time? $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}} 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 ']) Contin Ue $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 = '; privat e $password = '; Private $port = 21; Private $passive = TRUE; Private $debug = TRUE; Private $conn _id = FALSE; /** * constructor * @param array configuration: $config = array (' hostname ' => ', ' username ' => ', ' Password ' => ', ' Port ' => ' ...) ); */Public Function __construct ($config = Array ()) {if (count ($config) > 0) {$this->_init ($config);}}/** * FTP connection * @access Public * @param 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; /** * 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->c onn_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 Permissions List * @return Boolean/Public function mkdir ($ Path = ', $permissions = NULL) {if ($path = = ' OR! $this->_isconn ()) {return FALSE;} $result = @ftp_mkdir ($this-&G t;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); /** * 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 ', $permis sions = 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 Identity * @param boolean judge whether to rename (FALSE) or Move (TRU E) * @return Boolean/Public Function rename ($oldname, $newname, $move = FALSE) {if (! $this->_isconn ()) {return FA LSE; } $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 to the '/' character of the directory macro = Preg_replace ("/(. +?) /*$/"," 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 Delet E_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 ($path, $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->_error ("Ftp_unable_to_chmod (function)"); $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 ($config = Array ()) {F Oreach ($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, $t His->username, $this->password); /** * 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; /** * get suffix extension from filename * * @access private * @param string directory ID * @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 _setty PE ($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*/

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.