Move and entire folder's contents to another folder.

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
pyromaster114
Forum Newbie
Posts: 19
Joined: Fri Mar 14, 2008 8:12 pm

Move and entire folder's contents to another folder.

Post by pyromaster114 »

Okay so I have a php system I'm writing that needs to move an entire folder's contents of any number of files in it to another folder.
Basically, this is the file structure:
>Home
|
|=Dir1(source)
|
|=Dir2(destination)

I want it to take the stuff from Dir1 and move it to Dir2.

I've been stuck on this for hours... I've found several people's stuff that they claim works but I have not been able to use any of it...
Help would be greatly appreciated.
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Re: Move and entire folder's contents to another folder.

Post by alex.barylski »

http://aidanlister.com/repos/v/function.copyr.php

I believe there is a move equivelant somewhere as well.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: Move and entire folder's contents to another folder.

Post by John Cartwright »

pyromaster114
Forum Newbie
Posts: 19
Joined: Fri Mar 14, 2008 8:12 pm

Re: Move and entire folder's contents to another folder.

Post by pyromaster114 »

Hockey wrote:http://aidanlister.com/repos/v/function.copyr.php

I believe there is a move equivelant somewhere as well.
I can't get this to work... I've been trying for about 20 min. (found it earlier today...) and I cannot get it to work...
What am I doing wrong?

Code: Select all

<?php
/**
 * Copy a file, or recursively copy a folder and its contents
 *
 * @author      Aidan Lister <aidan@php.net>
 * @version     1.0.1
 * @link        http://aidanlister.com/repos/v/function.copyr.php
 * @param       string   $source    Source path
 * @param       string   $dest      Destination path
 * @return      bool     Returns TRUE on success, FALSE on failure
 */
$source = "../projects/4_what/";
$dest = "../projects/hey/";
function copyr($source, $dest)
{
    // Simple copy for a file
    if (is_file($source)) {
        return copy($source, $dest);
    }
 
    // Make destination directory
    if (!is_dir($dest)) {
        mkdir($dest);
    }
 
    // Loop through the folder
    $dir = dir($source);
    while (false !== $entry = $dir->read()) {
        // Skip pointers
        if ($entry == '.' || $entry == '..') {
            continue;
        }
 
        // Deep copy directories
        if ($dest !== "$source/$entry") {
            copyr("$source/$entry", "$dest/$entry");
        }
    }
 
    // Clean up
    $dir->close();
    return true;
}
 
?>
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: Move and entire folder's contents to another folder.

Post by John Cartwright »

The easiest way to accomplish this is using rename(), however if you insist otherwise..

Your code is not working because you haven't actually called the function.
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Re: Move and entire folder's contents to another folder.

Post by alex.barylski »

The easiest way to accomplish this is using rename(), however if you insist otherwise..
Hahaha. Touche. :P
Your code is not working because you haven't actually called the function.
Hahaha. That usually helps. :P
pyromaster114
Forum Newbie
Posts: 19
Joined: Fri Mar 14, 2008 8:12 pm

Re: Move and entire folder's contents to another folder.

Post by pyromaster114 »

Wow... now i feel stupid... yeah...
Alright I got it copying the stuff over correctly... now to delete the original...

Does anyone know of a script that deletes an entire folder with the files in it?
anto91
Forum Commoner
Posts: 58
Joined: Mon Mar 10, 2008 10:59 am
Location: Sweden

Re: Move and entire folder's contents to another folder.

Post by anto91 »

Just modifie your code

Code: Select all

 
<?php
/**
 * Copy a file, or recursively copy a folder and its contents
 *
 * @author      Aidan Lister <aidan@php.net>
 * @version     1.0.1
 * @link        http://aidanlister.com/repos/v/function.copyr.php
 * @param       string   $source    Source path
 * @param       string   $dest      Destination path
 * @return      bool     Returns TRUE on success, FALSE on failure
 */
$source = "../projects/4_what/";
$dest = "../projects/hey/";
function copyr($source, $dest)
{
    // Simple copy for a file
    if (is_file($source)) {
        return copy($source, $dest);
    }
 
    // Make destination directory
    if (!is_dir($dest)) {
        mkdir($dest);
    }
 
    // Loop through the folder
    $dir = dir($source);
    while (false !== $entry = $dir->read()) {
        // Skip pointers
        if ($entry == '.' || $entry == '..') {
            continue;
        }
 
        // Deep copy directories
        if ($dest !== "$source/$entry") {
            copyr("$source/$entry", "$dest/$entry");
            unlink($source.DIRECTORY_SEPARATOR.$entry);
        }
    }
 
    // Clean up
    $dir->close();
    rmdir($source);
    return true;
}
 
?>
OR if your hosting on a linux computer

Code: Select all

 
// MUST BE FULL DIRECTORY!
exec('rm -r /var/www/'. $source);
 
pyromaster114
Forum Newbie
Posts: 19
Joined: Fri Mar 14, 2008 8:12 pm

Re: Move and entire folder's contents to another folder.

Post by pyromaster114 »

Okay I got it to work with the deleting and moving copying things...

Another question... how do I go about putting in a varriable name to a file?
fwrite($fh, $stringData);
$stringData = "echo \"$maincontent1\"; \n";
The line originally to put into the file is echo "$maincontent";
how do I go about substituting an ascii code or something for the $ to make it work?
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Re: Move and entire folder's contents to another folder.

Post by RobertGonzalez »

unlink() and rmdir() might be helpful to you.
Post Reply