Php script for uploading files through ftp

Source: Internet
Author: User
Tags ftp connection ftp login
Php script for uploading files through ftp
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.

  1. Error_reporting (7 );
  2. If ($ _ SERVER ['server _ ADDR ']) exit; // disable running under the web SERVER
  3. $ _ GET ['clude'] = array('number.txt ', 'uploads', 'zend', 'docs', 'cache', 'you', 'managesdk '); // exclude the upload directory and define it as a global variable
  4. $ Fileobj = new FilerFile ();
  5. $ Path = "/data/longtu/"; // root directory of the Project directory
  6. $ Files = $ fileobj-> Zip ($ path); // filter out the latest modified 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); // link to the 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 whether a folder is used
  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 ['clude']) 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 the FTP class of CodeIgniter
  108. * Basic FTP operations:
  109. * 1) login; connect
  110. * 2) current directory file list; filelist
  111. * 3) Directory Change; chgdir
  112. * 4) rename/move; rename
  113. * 5) create a folder; mkdir
  114. * 6) delete; delete_dir/delete_file
  115. * 7) upload and upload
  116. * 8) 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
  130. *
  131. * @ Param array configuration array: $ 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 configure the array
  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. * Whether the folder exists
  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 ID (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 ID (ftp)
  200. * @ Param int: file permission 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 ID
  224. * @ Param string remote directory identification (ftp)
  225. * @ Param string: auto | ascii
  226. * @ Param int: List of uploaded file permissions
  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 ID
  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 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 an object
  308. *
  309. * @ Access public
  310. * @ Param string File ID (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 a folder
  328. *
  329. * @ Access public
  330. * @ Param string Directory ID (ftp)
  331. * @ Return boolean
  332. */
  333. Public function delete_dir ($ path ){
  334. If (! $ This-> _ isconn ()){
  335. Return FALSE;
  336. }
  337. // Add the backslash '\' to the '/' character of the directory macro '\'
  338. $ Path = preg_replace ("/(. + ?) \/* $/"," \ 1/", $ path );
  339. // Obtain the directory file list
  340. $ Filelist = $ this-> filelist ($ path );
  341. If ($ filelist! = False and count ($ filelist)> 0 ){
  342. Foreach ($ filelist as $ item ){
  343. // If the folder cannot be deleted, it may be a folder.
  344. // Recursively call delete_dir ()
  345. If (! @ Delete_file ($ item )){
  346. $ This-> delete_dir ($ item );
  347. }
  348. }
  349. }
  350. // Delete a 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 ID (ftp)
  365. * @ Return boolean
  366. */
  367. Public function chmod ($ path, $ perm ){
  368. If (! $ This-> _ isconn ()){
  369. Return FALSE;
  370. }
  371. // Only PHP5 defines the function for modifying permissions (ftp)
  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 the directory file list
  389. *
  390. * @ Access public
  391. * @ Param string Directory ID (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. * Disable 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 configure the array
  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. * Determine the 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 extension extensions from file names
  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. * Extended definition of FTP transmission mode ascii or binary from the suffix
  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. 'Phpml ',
  485. 'Shtml ',
  486. 'Log ',
  487. 'Xml'
  488. );
  489. Return (in_array ($ ext, $ text_type ))? 'Ascii ': 'binary ';
  490. }
  491. /**
  492. * Error log records
  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 */


Upload files, ftp, php

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.