PHP script for uploading files to FTP

Source: Internet
Author: User
Tags ftp connection ftp login rtrim ftp transfer
Presumably the principle traverses all non-excluded files in the project, and then gets文件修改时间晚于文件上一次修改时间The files then upload these files via FTP to the corresponding directory specific code as follows:

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

  1. Error_reporting (7);
  2. if ($_server[' server_addr ') exit;//prohibit running under the Web server
  3. $_get[' Exclude ' = Array (' Number.txt ', ' uploads ', ' Zend ', ' docs ', ' cache ', ' You ', ' managesdk '); Exclude upload directory, defined as global variable
  4. $fileobj = new Filerfile ();
  5. $path = "/data/longtu/"; The root directory of the project directory
  6. $files = $fileobj->zip ($path); Filter out the latest modification files
  7. $path = Str_replace ("/data/longtu/", "", $path);
  8. $config = Array (
  9. ' Hostname ' = ' xxx.xxx.xx.xxx ',//ftp server address
  10. ' username ' = ' xxx ',//ftp user
  11. ' Password ' = '? xxxxxxxxxxx ',//ftp password
  12. ' Port ' = 21//port
  13. );
  14. $FTP = new FTP ();
  15. $ftp->connect ($config);//Linked server
  16. $a = $ftp->filelist ();
  17. $LOCAL _root = Realpath (dirname (__dir__). /.. /.. /");
  18. ChDir ($LOCAL _root);
  19. foreach ($files as $k = = $v) {
  20. $f = $path. $v;
  21. $tmp = $ftp->upload ($f, $f);
  22. if ($tmp) {
  23. echo "Upload $f-success \ n";
  24. }
  25. }
  26. $ftp->download (' Ftp_upload.log ', ' ftp_download.log ');
  27. //
  28. $ftp->upload (' Ftp_err.log ', ' ftp_upload.log ');
  29. $ftp->download (' Ftp_upload.log ', ' ftp_download.log ');
  30. /*
  31. *
  32. *
  33. * $dir = "/test";
  34. if (@ftp_chdir ($conn, $dir))
  35. Determine if a folder
  36. * Enter description here ...
  37. * @author Administrator
  38. *
  39. */
  40. Class Filerfile
  41. {
  42. var $time _path;
  43. Private $fctimes = Array ();
  44. function Zip ($dir)
  45. {
  46. $this->time_path = RTrim ($dir, "/"). " /.~~~time.php ";
  47. @unlink ($this->time_path);
  48. $filelist = $this-Getfilelist ($dir);
  49. File_put_contents ($this->time_path, " fctimes,true).";
  50. return $filelist;
  51. }
  52. function Appendfilectime ($file)
  53. {
  54. $time _file_path = $this->time_path;
  55. $ftime = @include ($time _file_path);
  56. $ftime = $ftime? $ftime: Array ();
  57. $time = Filectime ($file);
  58. if (!file_exists ($time _file_path)) file_put_contents ($time _file_path, " }
  59. function Getfilebyfilectime ($file)
  60. {
  61. Static $time _data;
  62. $time _file_path = $this->time_path;
  63. if (! $time _data) {
  64. $time _data= @include_once ($time _file_path);
  65. }
  66. $time _data = $time _data? $time _data:array ();
  67. Var_dump ($file, $time _data[$file] = = Filectime ($file));
  68. echo $file. " \ t ". $time _data[$file]." \ n ";
  69. if ($time _data[$file] = = Filemtime ($file)) {
  70. return false;
  71. }else{
  72. return $file;
  73. }
  74. }
  75. function Getfilelist ($dir, $path = "")
  76. {
  77. Static $TMPP = "";
  78. if ($path = = "" &&! $tmpp) {
  79. $TMPP = $dir;
  80. }
  81. $d = Dir ($dir);
  82. $files = Array ();
  83. if ($d)
  84. {
  85. $pathP =str_replace ($tmpp, "", $dir);
  86. $pathP =str_replace (Array ("\\\\", "/"), Directory_separator, $pathP);
  87. $pathP =str_replace (directory_separator. Directory_separator,directory_separator, $pathP);
  88. while ($f = $d->read ())
  89. {
  90. if ($f = = '. ' | | $f = = '.. ' | | $f {0}== '. ' | | $f = = ' Zend ' | | in_array ($f, $_get[' Exclude ']) continue;
  91. $newdir = RTrim ($dir, "/"). " /". $f;
  92. if (Is_dir ($newdir)) {
  93. $files = Array_merge ($files, $this->getfilelist ($newdir, $newdir));
  94. }else{
  95. $abspath _file = (RTrim ($dir, "/")? RTrim ($dir, "/"). " /":"). $f;
  96. $this->fctimes[$abspath _file] = filemtime ($abspath _file);
  97. if (! $this->getfilebyfilectime ($abspath _file)) continue;
  98. $file = (RTrim ($pathP, "/")? RTrim ($pathP, "/"). " /":"). $f;
  99. $files [] = $file;
  100. }
  101. }
  102. }
  103. return $files;
  104. }
  105. }
  106. /**
  107. * Copy-write CodeIgniter FTP class
  108. * FTP basic operation:
  109. * 1) landing; Connect
  110. * 2) List of current directory files; FileList
  111. * 3) directory changes; Chgdir
  112. * 4) rename/move; Rename
  113. * 5) Create a folder; mkdir
  114. * 6) Delete; Delete_dir/delete_file
  115. * 7) Upload; Upload
  116. * 8) Download Download
  117. *
  118. * @author Quanshuidingdang
  119. */
  120. Class FTP {
  121. Private $hostname = ';
  122. Private $username = ';
  123. Private $password = ';
  124. Private $port = 21;
  125. Private $passive = TRUE;
  126. Private $debug = TRUE;
  127. Private $conn _id= FALSE;
  128. /**
  129. * Constructor function
  130. *
  131. * @param array configuration arrays: $config = array (' hostname ' = = ', ' username ' + ', ' password ' = ' = ', ' port ' = = ' ...);
  132. */
  133. Public function __construct ($config = Array ()) {
  134. if (count ($config) > 0) {
  135. $this->_init ($config);
  136. }
  137. }
  138. /**
  139. * FTP connection
  140. *
  141. * @access Public
  142. * @param array Configuration
  143. * @return Boolean
  144. */
  145. Public Function Connect ($config = Array ()) {
  146. if (count ($config) > 0) {
  147. $this->_init ($config);
  148. }
  149. if (FALSE = = = ($this->conn_id = @ftp_connect ($this->hostname, $this->port)) {
  150. if ($this->debug = = = TRUE) {
  151. $this->_error ("Ftp_unable_to_connect");
  152. }
  153. return FALSE;
  154. }
  155. if (! $this->_login ()) {
  156. if ($this->debug = = = TRUE) {
  157. $this->_error ("Ftp_unable_to_login");
  158. }
  159. return FALSE;
  160. }
  161. if ($this->passive = = = TRUE) {
  162. FTP_PASV ($this->conn_id, TRUE);
  163. }
  164. return TRUE;
  165. }
  166. /**
  167. * folder is present
  168. * @param unknown_type $path
  169. */
  170. Public Function is_dir_exists ($path)
  171. {
  172. return $this->chgdir ($path);
  173. }
  174. /**
  175. * Directory Change
  176. *
  177. * @access Public
  178. * @param string directory identifier (FTP)
  179. * @param boolean
  180. * @return Boolean
  181. */
  182. Public Function Chgdir ($path = ", $supress _DEBUG = FALSE) {
  183. if ($path = = "OR! $this->_isconn ()) {
  184. return FALSE;
  185. }
  186. $result = @ftp_chdir ($this->conn_id, $path);
  187. if ($result = = = FALSE) {
  188. if ($this->debug = = = TRUE and $supress _debug = = FALSE) {
  189. $this->_error ("ftp_unable_to_chgdir:dir[". $path. "]");
  190. }
  191. return FALSE;
  192. }
  193. return TRUE;
  194. }
  195. /**
  196. * Directory Generation
  197. *
  198. * @access Public
  199. * @param string directory identifier (FTP)
  200. * @param int file permissions List
  201. * @return Boolean
  202. */
  203. Public Function mkdir ($path = ", $permissions = NULL) {
  204. if ($path = = "OR! $this->_isconn ()) {
  205. return FALSE;
  206. }
  207. $result = @ftp_mkdir ($this->conn_id, $path);
  208. if ($result = = = FALSE) {
  209. if ($this->debug = = = TRUE) {
  210. $this->_error ("ftp_unable_to_mkdir:dir[". $path. "]");
  211. }
  212. return FALSE;
  213. }
  214. if (! Is_null ($permissions)) {
  215. $this->chmod ($path, (int) $permissions);
  216. }
  217. return TRUE;
  218. }
  219. /**
  220. * Upload
  221. *
  222. * @access Public
  223. * @param string Local directory identity
  224. * @param string Remote directory identification (FTP)
  225. * @param string upload mode auto | | Ascii
  226. * @param int file permission list after uploading
  227. * @return Boolean
  228. */
  229. Public function upload ($localpath, $remotepath, $mode = ' auto ', $permissions = NULL) {
  230. if (! $this->_isconn ()) {
  231. return FALSE;
  232. }
  233. if (! file_exists ($localpath)) {
  234. if ($this->debug = = = TRUE) {
  235. $this->_error ("Ftp_no_source_file:". $localpath);
  236. }
  237. return FALSE;
  238. }
  239. if ($mode = = ' auto ') {
  240. $ext = $this->_getext ($localpath);
  241. $mode = $this->_settype ($ext);
  242. }
  243. $mode = ($mode = = ' ASCII ')? Ftp_ascii:ftp_binary;
  244. $result = @ftp_put ($this->conn_id, $remotepath, $localpath, $mode);
  245. if ($result = = = FALSE) {
  246. if ($this->debug = = = TRUE) {
  247. $this->_error ("ftp_unable_to_upload:localpath[". $localpath. "] /remotepath[". $remotepath."] ");
  248. }
  249. return FALSE;
  250. }
  251. if (! Is_null ($permissions)) {
  252. $this->chmod ($remotepath, (int) $permissions);
  253. }
  254. return TRUE;
  255. }
  256. /**
  257. * Download
  258. *
  259. * @access Public
  260. * @param string Remote directory identification (FTP)
  261. * @param string Local directory identity
  262. * @param string download mode Auto | | Ascii
  263. * @return Boolean
  264. */
  265. Public function Download ($remotepath, $localpath, $mode = ' auto ') {
  266. if (! $this->_isconn ()) {
  267. return FALSE;
  268. }
  269. if ($mode = = ' auto ') {
  270. $ext = $this->_getext ($remotepath);
  271. $mode = $this->_settype ($ext);
  272. }
  273. $mode = ($mode = = ' ASCII ')? Ftp_ascii:ftp_binary;
  274. $result = @ftp_get ($this->conn_id, $localpath, $remotepath, $mode);
  275. if ($result = = = FALSE) {
  276. if ($this->debug = = = TRUE) {
  277. $this->_error ("ftp_unable_to_download:localpath[". $localpath. "] -remotepath[". $remotepath."] ");
  278. }
  279. return FALSE;
  280. }
  281. return TRUE;
  282. }
  283. /**
  284. * Rename/move
  285. *
  286. * @access Public
  287. * @param string Remote directory identification (FTP)
  288. * @param string new directory ID
  289. * @param Boolean to determine whether to rename (FALSE) or move (TRUE)
  290. * @return Boolean
  291. */
  292. Public function Rename ($oldname, $newname, $move = FALSE) {
  293. if (! $this->_isconn ()) {
  294. return FALSE;
  295. }
  296. $result = @ftp_rename ($this->conn_id, $oldname, $newname);
  297. if ($result = = = FALSE) {
  298. if ($this->debug = = = TRUE) {
  299. $msg = ($move = = FALSE)? "Ftp_unable_to_rename": "Ftp_unable_to_move";
  300. $this->_error ($msg);
  301. }
  302. return FALSE;
  303. }
  304. return TRUE;
  305. }
  306. /**
  307. * Delete Files
  308. *
  309. * @access Public
  310. * @param string file identifier (FTP)
  311. * @return Boolean
  312. */
  313. Public Function Delete_file ($file) {
  314. if (! $this->_isconn ()) {
  315. return FALSE;
  316. }
  317. $result = @ftp_delete ($this->conn_id, $file);
  318. if ($result = = = FALSE) {
  319. if ($this->debug = = = TRUE) {
  320. $this->_error ("ftp_unable_to_delete_file:file[". $file. "]");
  321. }
  322. return FALSE;
  323. }
  324. return TRUE;
  325. }
  326. /**
  327. * Delete Folder
  328. *
  329. * @access Public
  330. * @param string directory identifier (FTP)
  331. * @return Boolean
  332. */
  333. Public Function Delete_dir ($path) {
  334. if (! $this->_isconn ()) {
  335. return FALSE;
  336. }
  337. Add backslash ' \ ' to the '/' character of the directory macro
  338. $path = Preg_replace ("/(. +?) \/*$/"," \\1/", $path);
  339. Get catalog File List
  340. $filelist = $this->filelist ($path);
  341. if ($filelist!== FALSE and Count ($filelist) > 0) {
  342. foreach ($filelist as $item) {
  343. If we can't delete it, then it could be a folder
  344. So we recursively call Delete_dir ()
  345. if (! @delete_file ($item)) {
  346. $this->delete_dir ($item);
  347. }
  348. }
  349. }
  350. Delete folder (empty folder)
  351. $result = @ftp_rmdir ($this->conn_id, $path);
  352. if ($result = = = FALSE) {
  353. if ($this->debug = = = TRUE) {
  354. $this->_error ("ftp_unable_to_delete_dir:dir[". $path. "]");
  355. }
  356. return FALSE;
  357. }
  358. return TRUE;
  359. }
  360. /**
  361. * Modify File permissions
  362. *
  363. * @access Public
  364. * @param string directory identifier (FTP)
  365. * @return Boolean
  366. */
  367. Public function chmod ($path, $perm) {
  368. if (! $this->_isconn ()) {
  369. return FALSE;
  370. }
  371. A function (FTP) that modifies permissions is defined only in PHP5
  372. if (! function_exists (' Ftp_chmod ')) {
  373. if ($this->debug = = = TRUE) {
  374. $this->_error ("Ftp_unable_to_chmod (function)");
  375. }
  376. return FALSE;
  377. }
  378. $result = @ftp_chmod ($this->conn_id, $perm, $path);
  379. if ($result = = = FALSE) {
  380. if ($this->debug = = = TRUE) {
  381. $this->_error ("ftp_unable_to_chmod:path[". $path. "] -chmod[". $perm."] ");
  382. }
  383. return FALSE;
  384. }
  385. return TRUE;
  386. }
  387. /**
  388. * Get directory file list
  389. *
  390. * @access Public
  391. * @param string directory identifier (FTP)
  392. * @return Array
  393. */
  394. Public function filelist ($path = '. ') {
  395. if (! $this->_isconn ()) {
  396. return FALSE;
  397. }
  398. Return Ftp_nlist ($this->conn_id, $path);
  399. }
  400. /**
  401. * Close FTP
  402. *
  403. * @access Public
  404. * @return Boolean
  405. */
  406. Public function Close () {
  407. if (! $this->_isconn ()) {
  408. return FALSE;
  409. }
  410. Return @ftp_close ($this->conn_id);
  411. }
  412. /**
  413. * FTP member variable initialization
  414. *
  415. * @access Private
  416. * @param array Configuration
  417. * @return void
  418. */
  419. Private Function _init ($config = Array ()) {
  420. foreach ($config as $key = = $val) {
  421. if (Isset ($this-$key)) {
  422. $this $key = $val;
  423. }
  424. }
  425. Special character filtering
  426. $this->hostname = preg_replace (' |. +?:/ /| ', ', $this->hostname);
  427. }
  428. /**
  429. * FTP Login
  430. *
  431. * @access Private
  432. * @return Boolean
  433. */
  434. Private Function _login () {
  435. Return @ftp_login ($this->conn_id, $this->username, $this->password);
  436. }
  437. /**
  438. * Judge con_id
  439. *
  440. * @access Private
  441. * @return Boolean
  442. */
  443. Private Function _isconn () {
  444. if (! Is_resource ($this->conn_id)) {
  445. if ($this->debug = = = TRUE) {
  446. $this->_error ("ftp_no_connection");
  447. }
  448. return FALSE;
  449. }
  450. return TRUE;
  451. }
  452. /**
  453. * Get the suffix extension from the file name
  454. *
  455. * @access Private
  456. * @param string Directory ID
  457. * @return String
  458. */
  459. Private Function _getext ($filename) {
  460. if (FALSE = = = Strpos ($filename, '. ')) {
  461. return ' txt ';
  462. }
  463. $extarr = Explode ('. ', $filename);
  464. Return end ($extarr);
  465. }
  466. /**
  467. * Definition of FTP transfer mode ASCII or binary from suffix extension
  468. *
  469. * @access Private
  470. * @param string suffix extension
  471. * @return String
  472. */
  473. Private Function _settype ($ext) {
  474. $text _type = Array (
  475. ' TXT ',
  476. ' Text ',
  477. ' PHP ',
  478. ' Phps ',
  479. ' PhP4 ',
  480. ' JS ',
  481. ' CSS ',
  482. ' HTM ',
  483. ' HTML ',
  484. ' Phtml ',
  485. ' sHTML ',
  486. ' Log ',
  487. ' XML '
  488. );
  489. Return (In_array ($ext, $text _type))? ' ASCII ': ' binary ';
  490. }
  491. /**
  492. * ERROR log record
  493. *
  494. * @access Prvate
  495. * @return Boolean
  496. */
  497. Private Function _error ($msg) {
  498. 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);
  499. }
  500. }
  501. /*end of File ftp.php*/
  502. /*location/apache group/htdocs/ftp.php*/
Copy Code
Upload files, FTP, PHP
  • Related Article

    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.