Solved

php move all files from one folder to another

Posted on 2009-05-18
3
3,459 Views
Last Modified: 2013-12-12
hello again...

i'm needing to add all contents from one folder to another using php. then delete the source.
source folder holds over 6,000 files..

does anybody have any ideas?

Ive tried the following with no luck..


<?

$directoryToMove = opendir("../folder1/innerFolder/");

// get each entry

while($entryName = readdir($directoryToMove)) {

	rename("sourcePath;", "newPath/");

}
 
 

?>

Open in new window

0
Comment
Question by:Cybervanes
3 Comments
 
LVL 4

Accepted Solution

by:
bljak earned 500 total points
ID: 24418749

<?

$sourceDir = "../folder1/innerFolder/";

$targetDir = "../folder2/innerFolder/";
 

if ( $dh = opendir($sourceDir) )

{

    while(false !== ($fileName = readdir($dh)))

    {

        if (!in_array($fileName, array('.','..')))

        {

            rename($sourceDir.$fileName, $targetDir.$fileName);

        }

    }

}

 

 

?>

Open in new window

0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 24425170
Can you just rename the folder?  Or are you adding files to an existing folder?  What do you want to do about filename collisions?  Keep the oldest or the newest, or overwrite the older with the new?

6,000 files is probably no trouble at all unless these are gigantic files.
0
 
LVL 2

Author Closing Comment

by:Cybervanes
ID: 31582871
Worked Good Thank You!
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

705 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

17 Experts available now in Live!

Get 1:1 Help Now