PHP writes a file multi-server synchronization program,
The examples in this article share with you the PHP file multi-server synchronization tool. The specific content is as follows:
<? Phpheader ('content-type: text/html; charset = UTF-8 '); // file name: php file multi-server synchronization tool // file function: the server where the file is located is the master server, the server where other files are stored can be used for shunting or backup. This file requires the jQuery library. You can also chain others' // last modification time: 2016-06-30 // BY: strwei if (! Empty ($ _ POST ['files']) sync (); function sync () {$ aFiles = explode ("\ n ", $ _ POST ['files ']); // var_dump ($ aFiles); exit; foreach ($ aFiles as $ k =>v v) {if (strlen ($ v) <= 5) unset ($ aFiles [$ k]);} array_filter ($ aFiles); if (empty ($ aFiles) {echo 'no file'; exit ;} $ aFTPServers = array ('server1' => array ('strwei. com ', 'ftp Username', 'ftp password', 'default directory when FTP is enabled '), // FTP address, username, password, default directory when FTP opens this site // 'server2' => array ('yy. 9.je ', 'Ftp username', 'ftp password', 'default directory when FTP is enabled'),); $ aAbortFile = array('robots.txt '); $ aErrors = array (); $ iError = count ($ aErrors); $ aSuc = array (); foreach ($ aFTPServers as $ ftp) {$ conn = ftp_connect ($ ftp [0]); if (empty ($ conn) {$ iError = $ iError + 1; array_push ($ aErrors, $ iError. ", the server $ ftp cannot be connected"); continue;} $ login = ftp_login ($ conn, $ ftp [1], $ ftp [2]); if (empty ($ login) {$ iError = $ iError + 1; array_pus H ($ aErrors, $ iError. ", server $ ftp [0] cannot log on"); continue;} $ ftp_root = ''; if (! Empty ($ ftp [3]) $ ftp_root = ftp_chdir ($ conn, $ ftp [3]); $ ftp_root = ftp_pwd ($ conn); if (substr ($ ftp_root, -1) = '/') $ ftp_root = substr ($ ftp_root, 0,-1); foreach ($ aFiles as $ k => $ v) {$ s = basename ($ v); if (in_array ($ s, $ aAbortFile) {$ iError = $ iError + 1; array_push ($ aErrors, $ iError. ", file $ v cannot be synchronized"); continue;} if (! File_exists ('. '. $ v) {$ iError = $ iError + 1; array_push ($ aErrors, $ iError. ", file $ v does not exist"); continue;} $ upload = ftp_put ($ conn, $ ftp_root. $ v ,'. '. $ v, FTP_BINARY); if (empty ($ upload) {$ iError = $ iError + 1; array_push ($ aErrors, $ iError. ", file $ v Upload Failed"); continue;} array_push ($ aSuc, $ v) ;}} if (empty ($ aErrors) {echo 'uploaded successfully :'. implode (',', $ aSuc); exit;} echo implode ('<br>', $ aErrors); exit;} $ Cc = '\ r \ n'; echo' <script src = "// cdn.bootcss.com/jquery/3.0.0/jquery.js"> </script> '; echo <EOF, enter the file name to be synchronized (including its path), for example, inoutcome_final.zip and/default. php,/index. php,/inoutcome_final.sqlite, etc. Enter a file name in each line and press enter to enter multiple <br> <textarea style = "width: pixel PX; height: 85px; border: 1px #000 solid; "id =" id_file "onkeyup =" this. value = this. value. replace (// g, '');"> </textarea> <br> <button id = "btn_submit" onclick = "return Submit_sync (); "> synchronize the above files </button> <br> <span id =" show_sync "style =" color: # f00; "> </span> <script> function submit_sync () {$ (" # btn_submit "). hide (); var sFiles =$ ("# id_file "). val (); var aFiles = sFiles. split (/$ cc/); if (aFiles. length <= 0 | aFiles [0]. length <= 5) {alert ("Please manually write the file list to be synchronized"); $ ("# btn_submit "). show ("slow"); return false ;}$. post ("? ", {" Files ": sFiles}, function (v) {$ (" # show_sync "). text (v); if (v. indexOf (" uploaded successfully ")! =-1) $ ("# id_file "). val (''); $ (" # btn_submit "). show (5000) ;}) return true ;}</script> EOF;?>
The above is all the content of this article. I hope it will help you learn php programming.