Page 1 of 1

Move and entire folder's contents to another folder.

Posted: Fri Mar 14, 2008 8:21 pm
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.

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

Posted: Fri Mar 14, 2008 8:27 pm
by alex.barylski
http://aidanlister.com/repos/v/function.copyr.php

I believe there is a move equivelant somewhere as well.

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

Posted: Fri Mar 14, 2008 8:41 pm
by John Cartwright

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

Posted: Fri Mar 14, 2008 8:42 pm
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;
}
 
?>

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

Posted: Fri Mar 14, 2008 9:01 pm
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.

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

Posted: Fri Mar 14, 2008 9:08 pm
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

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

Posted: Sat Mar 15, 2008 1:49 am
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?

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

Posted: Sat Mar 15, 2008 5:28 am
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);
 

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

Posted: Sat Mar 15, 2008 4:07 pm
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?

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

Posted: Sun Mar 16, 2008 12:06 am
by RobertGonzalez
unlink() and rmdir() might be helpful to you.