?
Solved

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

Posted on 2011-02-28
5
Medium Priority
?
145 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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
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.
Suggested Courses

771 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