Moving All Files To A New Folder With PHP

Copying All Files To a New Folder With PHP

The simple objective is to move all files from one folder to another another folder. The files could be images, css files, javascript files, or anything else.

Let's analyze the code. The variable $mydir is the name of your new directory where you want to move files. If there is no directory, we make the directory with the mkdir() function. Now that the directory is in place, the files must be moved from point a to point b. The glob() function is used to make an array of all the files from the current images directory where the original images are located. Then, a foreach loop is used to move each file from the array into the new folder. The copy() function actually moves the files from one directory to another.

 // images folder creation
	   $mydir = dirname( __FILE__ )."/html/images";
	   // Move all images files
	   $files = glob("images/*.*");
	   foreach($files as $file){
	   $file_to_go = str_replace("images/","html/images/",$file);
       copy($file, $file_to_go);