Solved

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

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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
PHP and Soap 3 30
Use PHP to save or display image returned by JSON call. 20 51
Time difference 10 36
html input clean up 3 32
Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

746 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now