| | |
uploaded file moving to new directory
Please support our PHP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
![]() |
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:
where path/to/ is the path to the folders.
Heres a reference:
http://pangea.stanford.edu/computeri.../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*
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:
PHP Syntax (Toggle Plain Text)
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/computeri.../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*
www.fijiwebdesign.com - web design and development and fun
Cpanel Email - Let users Register email accounts on your website upon registration
Ajax Chat - Fully browser based chat!
Cpanel Email - Let users Register email accounts on your website upon registration
Ajax Chat - Fully browser based chat!
•
•
Join Date: May 2006
Posts: 2
Reputation:
Solved Threads: 0
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
::waiting for your replay
Thanks
Shanee
My Home Page http://www.jokespk.com
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...
[PHP]
/**
* 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;
}
}
[/PHP]
Heres an example of how to use it based on your context.
[PHP]
/**
* 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
}
[/PHP]
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.
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...
[PHP]
/**
* 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;
}
}
[/PHP]
Heres an example of how to use it based on your context.
[PHP]
/**
* 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
}
[/PHP]
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.
www.fijiwebdesign.com - web design and development and fun
Cpanel Email - Let users Register email accounts on your website upon registration
Ajax Chat - Fully browser based chat!
Cpanel Email - Let users Register email accounts on your website upon registration
Ajax Chat - Fully browser based chat!
![]() |
Similar Threads
- Iptables (*nix Software)
- uploaded file origin directory (PHP)
- Sendmail and SquirrelMail problem. (Linux Servers and Apache)
Other Threads in the PHP Forum
- Previous Thread: PHP to upload and retrieve image to/from MySQL
- Next Thread: optional variables in php functions
| Thread Tools | Search this Thread |
# 5.2.10 action address apache api array auto autoincrement beginner binary broken cakephp checkbox class classes cms code cron curl database date dehasher destroy display dissertation domain dynamic echo echo$_get[x]changingitintovariable... email error errorlog fatalerror file files folder form forms function functions google href htaccess html if-else image images include insert ip javascript joomla legislation limit link load login mail masterthesis menu mlm multiple mysql mysqlquery oop open paypal pdf persist php popup problem query radio random record recursion remote script search server sessions sms sockets source space sql syntax system table tutorial update upload url validator variable video web youtube






