Move and entire folder's contents to another folder.
Moderator: General Moderators
-
pyromaster114
- Forum Newbie
- Posts: 19
- Joined: Fri Mar 14, 2008 8:12 pm
Move and entire folder's contents to another folder.
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.
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.
http://aidanlister.com/repos/v/function.copyr.php
I believe there is a move equivelant somewhere as well.
I believe there is a move equivelant somewhere as well.
- John Cartwright
- Site Admin
- Posts: 11470
- Joined: Tue Dec 23, 2003 2:10 am
- Location: Toronto
- Contact:
-
pyromaster114
- Forum Newbie
- Posts: 19
- Joined: Fri Mar 14, 2008 8:12 pm
Re: Move and entire folder's contents to another folder.
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...Hockey wrote:http://aidanlister.com/repos/v/function.copyr.php
I believe there is a move equivelant somewhere as well.
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;
}
?>- 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.
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.
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.
Hahaha. Touche.The easiest way to accomplish this is using rename(), however if you insist otherwise..
Hahaha. That usually helps.Your code is not working because you haven't actually called the function.
-
pyromaster114
- Forum Newbie
- Posts: 19
- Joined: Fri Mar 14, 2008 8:12 pm
Re: Move and entire folder's contents to another folder.
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?
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.
Just modifie your code
OR if your hosting on a linux computer
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;
}
?>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.
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?
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?
- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA