PHP recursively replicates folders and transfers folders to other servers.

Source: Internet
Author: User
Tags rtrim

You need to replicate the entire folder in your project, and sometimes you need to transfer the entire folder to a remote server.

This is the recursive traversal of the entire folder, you want to see the recursive traversal of the folder code.

functionDeepscandir ($dir) {    $FILEARR=Array (); $DIRARR=Array (); $dir=RTrim($dir, ‘//‘); if(Is_dir($dir)) {        $dirHandle=Opendir($dir);  while(false!== ($fileName=Readdir($dirHandle))) {            $subFile=$dir. Directory_separator.$fileName; if(Is_file($subFile)) {                $FILEARR[] =$subFile; }            ElseIf(Is_dir($subFile) &&Str_replace(‘.‘, ‘‘,$fileName)! = ") {                $DIRARR[] =$subFile; $arr= Deepscandir ($subFile); $DIRARR=Array_merge($DIRARR,$arr[' dir ']); $FILEARR=Array_merge($FILEARR,$arr[' File ']); }        }        Closedir($dirHandle); }    return Array (        ' Dir ' =$DIRARR, ' file ' =$FILEARR    );}
The above iterates through a folder and returns the entire folder and file. copying folders and transferring folders is done on the basis of this method. To copy the folder first, in addition to the above traversal method, the Copy folder is mainly used in PHP copy ($source, $destination) function. $source is the folder you want to copy, $destination is the destination where the files are copied . 
functionCopy_dir ($dir,$todir) {    $dir=RTrim($dir, ‘//‘); if(Is_dir($dir)) {        $path=$todir." /".basename($dir); Mdir ($path, 0777);//Create a folder        $dirHandle=Opendir($dir);  while(false!== ($fileName=Readdir($dirHandle))) {            $subFile=$dir. Directory_separator.$fileName; if(Is_file($subFile)) {                $b=$path." /".$fileName; Copy($subFile,$b); }            ElseIf(Is_dir($subFile) &&Str_replace(‘.‘, ‘‘,$fileName)! = ") {$c=$path." /".basename($subFile) Mdir ($c, 0777);//Create a folderCopy_dir ($subFile,$path); }        }        Closedir($dirHandle); }} 

The above is the Replication folder, and the next attempt is to transfer the folder to the remote server. Here I use Linux under the PHP_SSH2 for transmission (FTP can also, here is not speaking)

$ssh 2= Ssh2_connect ($ssh _host,$ssh _port);//SSH to the Linux server first, here to skip, specific methods refer to another blog post http://www.cnblogs.com/suiyuewuxin/p/6858105.htmlfunctionScp_dir ($dir,$todir,$ssh 2) {    $dir=RTrim($dir, ‘//‘); if(Is_dir($dir)) {        $path=$todir." /".basename($dir); $c= "Mkdir-m 777".$path; Ssh2_exec ($ssh 2,$c); $dirHandle=Opendir($dir);  while(false!== ($fileName=Readdir($dirHandle))) {            $subFile=$dir. Directory_separator.$fileName; if(Is_file($subFile)) {                $b=$path." /".$fileName; Ssh2_scp_send ($ssh 2,$subFile,$b, 0644); }            ElseIf(Is_dir($subFile) &&Str_replace(‘.‘, ‘‘,$fileName)! = ") {                $c= "Mkdir-m 777".$path." /".basename($subFile); Ssh2_exec ($ssh 2,$c); Scp_dir ($subFile,$path,$ssh 2); }        }        Closedir($dirHandle); }}

This is combined with the PHP_SSH2 component and the Scp_scp_send method for recursive transfer, creating a folder when a folder is encountered, and transferring files when a file is encountered.

The following is a more granular, recursive transfer of all files under a folder to the target folder.

functionScp_send ($dir,$todir,$ssh 2){    $dirHandle=Opendir($dir);  while(false!== ($fileName=Readdir($dirHandle))) {            $subFile=$dir. Directory_separator.$fileName; if(Is_file($subFile)) {                $b=$todir." /".$fileName; $stream=ssh2_scp_send ($ssh 2,$subFile,$b, 0644); }            ElseIf(Is_dir($subFile) &&Str_replace(‘.‘, ‘‘,$fileName)! = ") {                $c= "Mkdir-m 777".$dir." /".basename($subFile); Ssh2_exec ($ssh 2,$c); Scp_dir ($subFile,$todir,$ssh 2); }        }    Closedir($dirHandle);}

This method takes advantage of the previous method, but the outermost folder is not transmitted in the past, which can be used to create a new project.

It is unclear in the code to refer to several other posts about SSH2 and SCP.

PHP recursively replicates folders and transfers folders to other servers.

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.