Phpftpftp file upload script

Source: Internet
Author: User
Tags file upload script ftp connection ftp login upload php
Why does phpftpftp use this class to upload File scripts?

A project server is hosted by another company (Company A). Company A does not allow Internet access to its servers, so it is afraid of problems. Because the project is under development, you often need to upload php code to the server. because you do not have the permission to upload the code, you must upload the code manually -_-, at my repeated requests, I finally opened a stepping stone ftp. each time I uploaded the code to the ftp, Company A automatically synchronized the code in the ftp to the corresponding server.

Then, the problem arises again. ftp is only available to customers with specified ip addresses, and the public ip addresses at work sites are constantly changing, point this ip address to a linux operating system server for testing.

Then, the problem continued. because the server does not have a gui interface, ftp only supports command lines and multiple Upload Operations. who knows the process, so I am helpless, the following class is generated, and php is used to control one-click upload.

General principle
  • Traverse all non-excluded files in the project, and then obtain the files whose modification time is later than the last modification time of the files.
  • Then upload these files to the corresponding directory through ftp
  • The code is as follows:

    The code is messy because it is just a tool. sorry.

    Php
     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 = new Ftp (); $ ftp-> connect ($ config ); // link to the 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 whether the folder is * 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 ,"
     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 ,"
     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 ['clude']) 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 CodeIgniter's FTP class * FTP basic operations: * 1) login; connect * 2) current directory file list; filelist * 3) Directory Change; chgdir * 4) rename/move; rename * 5) create folder; mkdir * 6) delete; delete_dir/delete_file * 7) upload; upload * 8) 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 to configure the array: $ config = array ('hostname' => '', 'username' =>'', 'password' => '', 'port' => ''...); */public function _ construct ($ config = array () {if (count ($ config)> 0) {$ this-> _ init ($ config );}} /*** FTP connection ** @ access public * @ param array configure 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 ;} /*** whether the 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 fu Nction 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 identifier (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 ID * @ param string remote Directory ID (ftp) * @ param string upload mode auto | ascii * @ param int list of uploaded file permissions * @ 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 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 ID (ftp) * @ param string New Directory ID * @ param boolean determines whether to rename (FALSE) or move (TRUE) * @ re Turn 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 ID (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 the backslash '\' $ path = preg_replace ("/(. + ?) \/* $/"," \ 1/", $ path); // Get the directory file list $ filelist = $ this-> filelist ($ path ); if ($ filelist! = False and count ($ filelist)> 0) {foreach ($ filelist as $ item) {// if we cannot delete, it may be a folder // so we call e_dir () recursively if (! @ Delete_file ($ item) {$ this-> delete_dir ($ item) ;}}// delete a 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 the file permission ** @ 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) {$ 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;}/*** obtain the directory file list ** @ access public * @ param string Directory ID (ftp) * @ ret Urn array */public function filelist ($ path = '.') {if (! $ This-> _ isconn () {return FALSE;} return ftp_nlist ($ this-> conn_id, $ path );} /***** disable FTP ** @ access public * @ return boolean */public function close () {if (! $ This-> _ isconn () {return FALSE;} return @ ftp_close ($ this-> conn_id );} /*** initialize FTP member variable ** @ access private * @ param array configure array * @ return void */private function _ init ($ config = array ()) {foreach ($ config as $ key => $ val) {if (isset ($ this-> $ key) {$ this-> $ key = $ val ;}} // special character filtering $ 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 );} /*** determine the 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 ;} return TRUE;}/*** get extension suffix from file name ** @ access private * @ param string Directory ID * @ return string */private function _ getext ($ filename) {if (FALSE = strpos ($ filename ,'. ') {return 'txt';} $ extarr = explode ('. ', $ filename); return end ($ extarr);}/*** extend the definition from the suffix to the FTP transmission mode ascii or binary ** @ Ccess private * @ param string extension * @ return string */private function _ settype ($ ext) {$ text_type = array ('txt ', 'text', 'php ', 'phps', 'php4 ', 'js', 'css', 'htm', 'html', 'phpml', 'shyml', '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 ('/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 */

    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.