Solved

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

Posted on 2011-02-28
5
142 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
These days socially coordinated efforts have turned into a critical requirement for enterprises.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

809 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