Solved

How to copy folder and files within same or different server with php code?

Posted on 2011-02-28
5
140 Views
Last Modified: 2013-11-30
Hello,

i want to copy files/subfolder from one folder to another folder and replace existing one.


is it also possible if server or domain name is different?
0
Comment
Question by:savsofts
5 Comments
 
LVL 27

Expert Comment

by:Lukasz Chmielewski
ID: 34995154
0
 

Author Comment

by:savsofts
ID: 34995236
i am using below code but it doesn't work..



$src='folder1';
$dst='folder2';

    function recurse_copy($src,$dst) {
    $dir = opendir($src);
    @mkdir($dst);
    while(false !== ( $file = readdir($dir)) ) {
        if (( $file != '.' ) && ( $file != '..' )) {
            if ( is_dir($src . '/' . $file) ) {
                recurse_copy($src . '/' . $file,$dst . '/' . $file);
            }
            else {
                copy($src . '/' . $file,$dst . '/' . $file);
            }
        }
    }
    closedir($dir);
}


0
 
LVL 8

Accepted Solution

by:
ropenner earned 250 total points
ID: 34995272
I'm not sure which example you need so I give them all.

**** This is what I think you are looking for ****
Renaming all files and folders within a folder by moving each individually to another folder.
<?PHP
      $path = '/tmp/test/*';
      $newplace = '/tmp/goodfolder/";
      $files = glob($path);
      foreach ($files as $file) {
            $info=pathinfo($file);// basename($file) also works then the next line isn't needed
            $base = info{'basename'};
            $dir = info{'dirname'};
            rename("$file", "$newplace/$base");
            print "tried to rename [$file] as [$newplace/$base]<BR>\n";
      }
?>


just renaming the folder does what will be effectively the same as copying all files to another folder with another name.

If you still need the original folder then you can re-create it with.
     mkdir("pathname");

LINUX/UNIX example of renaming a folder:
     $from = "/home/user1/junkfolder";
     $to = "/home/user1/goodfolder";
     $result = @rename($from, $to);

WINDOWS example of renaming a folder:
     $from = "C:/Documents and Settings/xxxxxx/My Documents/My Music/temp";
     $to = "C:\\temp\\dest2\\tempo";
     $result = @rename("$from", "$to");# worked because tempo didn't exist


FROM this question on copying files from one computer to another over the network.  Summary:  it cannot be done in windows but UNIX/LINUX can allow it as can HTTP.  However you cannot rename HTTP files or move or rename them ... only copy them to your current location and in some cases to another HTTP location.

     copy("http://somecomputeronmynetwork/file","c:/temp/myfile");

The following question link elaborates on this discussion of copying files over network resources.
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/PHP_Windows/Q_26402003.html?cid=1572#a33449468

"there is no way to do this through apache that I see... even setting the HTTP process user to my username (do this in control panel->administrative tools->services->wampapache) there was no way to give the PHP process access to network resources in windows.  I believe I have exhausted all possibilities.

If you just want to see network resources and copy them ... but not rename or move folders then.... you can setup a webserver on your remote computer and serve files that way and copy them to the PHP computer with.

copy("http://somecomputeronmynetwork/file","c:/temp/myfile");

"
0
 
LVL 16

Expert Comment

by:CWS (haripriya)
ID: 39686717
This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.
0

Featured Post

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
This article discusses how to create an extensible mechanism for linked drop downs.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question