Hello to all
i have two directories at my webserver
1st = old-pictures
2nd = new-pictures

i want to move every file individually to the 2nd directory with the help of php, can anyone help me in this problem
thanks
shanee

Hi shanee,

I think its better if you try to use unix shell instead of php to copy the contents for the folder to the new one.

Something like:

cp path/to/old-pictures/* path/to/new-pictures

where path/to/ is the path to the folders.

Heres a reference:
http://pangea.stanford.edu/computerinfo/unix/shell/commands.html

If you dont have shell access then you can use myshell, which uses php to give you a shell like interface.
http://www.digitart.com.mx/php/myshell/

I'd warn you to be careful when you use either of the above though, or even file management functions with php, cause when something goes wrong... you probably wont be able to fix it.
Please dont blame me if anything goes wrong* ;)

dear digital-ether thanks for the replay, but i want to done the task with php. because i need that for the CMS.....

::waiting for your replay

Thanks
Shanee

HI shanee,

I wrote up a class that will do the job of copying all the files in a folder.
Its simpler with a class cause you can set different configurations for copying and maybe extend it to your own needs.
The class has the basic functionality...

/**
* Copies a Directory Contents to new Directory
*/
class copyDir {

	var $dir1;
	var $dir2;
	var $errtxt;
	var $overwrite_files;
	var $recurse;
	var $copy_callback;
	var $copy;
	
	function copyDir() {
		$this->overwrite_files = false;
		$this->recursive = false;
		$this->copy_callback = false;
		$this->copy = new stdClass();
	}
	
	/**
	* Set the directory to copy from
	*/
	function setCopyFromDir( $dir ) {
		if (!is_dir($dir)) {
			$this->_error('The supplied argument, '.$dir.', is not a valid directory path!');
			return false;
		}
		$this->dir1 = $dir;
		return true;
	}
	
	/**
	* Set the directory to copy to
	*/
	function setCopyToDir( $dir ) {
		if (!is_dir($dir)) {
			if (!mkdir($dir)) {
				$this->_error('Could not create directory, '.$dir);
				return false;
			}
		}
		$this->dir2 = $dir;
		return true;
	}
	
	/**
	* Set if we want to copy sub folders or not
	* @param bool TRUE or FALSE
	*/
	function copySubFolders( $bool = true ) {
		$this->recurse = $bool;
	}
	
	/**
	* Set if we want to overwrite existing files or not
	* @param bool TRUE or FALSE
	*/
	function overWriteFiles( $bool = true ) {
		$this->overwrite_files = $bool;
	}
	
	/**
	* Set a callback function to be executed each time a file or sub folder is copied
	* @param string callback function name
	*/
	function setCopyCallback( $fn ) {
		$this->copy_callback = $fn;
	}
	
	/**
	* Create the directory copy
	* @param bool Recurse through sub directories?
	*/
	function createCopy() {
		if ( !is_dir($this->dir1) ) {
			$this->_error('Directory to copy from is not a directory. Set the directory using setCopyFromDir()');
			return false;
		}
		if ( !is_dir($this->dir2) ) {
			$this->_error('Directory to copy to is not a directory. Set the directory using setCopyToDir()');
			return false;
		}
		
		// all set, start copy
		if ($this->_makeCopy($this->dir1, $this->dir2, $recurse)) {
			return true;
		}
		return false;
	}
	
	function _makeCopy($dir1, $dir2) {
		if ($dh = opendir($dir1)) {
			$i = 0;
			$dirArr = array();
			while ($el = readdir($dh)) {
				$path1 = $dir1.'/'.$el;
				$path2 = $dir2.'/'.$el;
				$this->_setCopyStatus($path1, $path2);

				if (is_dir($path1) && $el != '.' && $el != '..') {
					if (is_dir($path2)) {
						continue;
					}
					if (!mkdir($path2)) {
						$this->_error('Could not create new directory, '.$path2);
						return false;
					}
					if ($this->recurse) { // copy sub directories recursively
						_makeCopy($path1, $path2);
					}
				} elseif (is_file($path1)) {
					if (is_file($path2) && !$this->overwrite_files) {
						$this->_error("Duplicate File Exists, when trying to copy file: $path1, to $path2.<br /> Set overWriteFiles() if you need to overwrite existing files.");
						return false;
					}
					if (!copy($path1, $path2)) {
						$this->_error('Could not copy file, '.$path1.', to '.$path2);
						return false;
					}
				}
				$i++;
			}			   
		   closedir($dh);
		   return true;
	   	} else {
			$this->_error('Could not open the directory, '.$dir1);
		}
		return false;
	}
	
	function _setCopyStatus($path1, $path2) {
		$this->copy->oldfile = $path1;
		$this->copy->newfile = $path2;
		$this->_callback();
	}
	
	function _callback() {
		if ($this->copy_callback) {
			call_user_func($this->copy_callback, $this->copy);
		}
	}
	
	function _error($txt) {
		$this->errtxt = $txt;
	}
	
	/**
	* View the last error logged
	*/
	function viewError() {
		return $this->errtxt;
	}
	
	
}

Heres an example of how to use it based on your context.

/**
* Usage of the copy directory class
*/

$dir = dirname(__FILE__); // the directory this script is in, just for reference

$cp = new copyDir();

// set the directory to copy, make sure this is the full path to the folder you want to copy
if (!$cp->setCopyFromDir( $dir.'/old-pictures' )) {
	echo $cp->viewError();
	die;
}

// set the directory to copy to
if (!$cp->setCopyToDir( $dir.'/new-pictures' )) {
	echo $cp->viewError();
	die;
}

$cp->copySubFolders(true); // include sub folders when copying
//$cp->overWriteFiles(true); // overwrite existing files

$cp->setCopyCallback( 'updateCopyProgress' ); // set a function to callback each time a file is copied, or a new directory created
// remember to define the callback function

if (!$cp->createCopy( true )) { // create a copy and recurse through sub folders
	echo $cp->viewError();
	die;
}

// this is our custom function which is called each time we are about to make a new file copy
// or create a new sub directory
function updateCopyProgress(&$copy) {
	$file1 = $copy->oldfile;
	$file2 = $copy->newfile;
	$type = is_dir($file1) ? 'dir' : 'file';
	
	if ($type == 'dir') {
		echo "Creating new Directory, $file2<br />";
	} else {
		echo "Copying $file1 to $file2<br />";
	}
	flush(); // send this to the browser
	
}

I believe theres a few classes like these out there, you can probably get some ideas from them also.
I wrote this class so that you can also keep track of the progress of the copy, if its a large folder. This is done with the setCopyCallback() function. Which you use to define your own custom function to keep track of the copy. In the above example I defined "updateCopyProgress" as the function to callback each time a new file is copied, or a directory created.

The callback function is given a parameter, which is an stdClass (standard php Class Object.. I think...) which has the properties oldfile and newfile.
eg: $copy->newfile, $copy->oldfile
You can use these two variables to keep track of the progress, even calculate time left etc.
Functions like: filesize(), http://www.php.net/manual/en/function.filesize.php, can be used for this. And also by generating a timestamp when the copying starts, and measuring time elapsed between each file copied... etc.

Heres the resource of functions used in the class:
http://www.php.net/manual/en/ref.filesystem.php

hope that helps.

Wow, they didn't even reply to your thread, I found your script useful. Thanks

Distance

hi i m new in this forum i want to say that i m agree with digital-ether........

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.