Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2011-02-28
5
Medium Priority
?
148 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 1000 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
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…
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.

636 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