note 61152 added to function.rename

From: Date: Thu, 26 Jan 2006 20:30:50 +0000
Subject: note 61152 added to function.rename
Groups: php.notes 
Request: Send a blank email to php-notes+get-102514@lists.php.net to get a copy of this message
I have programmed a really nice program that remotely lets you manage files as if you have direct access to them (http://sourceforge.net/projects/filemanage/). I have a bunch of really handy functions to do just about anything to files or directories. In it I just finished redevloping the directory move function to utilize PHP's rename() since it is way more efficient than a copy/delete process. It goes through (recursivly) and renames all the files to a new location instead of copying them. It recreates the directory structure in the new location. It also allows you to overwrite the existing files, or not. Here is the function I made; it will likely need tweaking to work as a standalone script, since it relies of variables set by my program (eg: loc1 -- which dynamically changes in my program): <?PHP // loc1 is the path on the computer to the base directory that may be moved define('loc1', 'C:/Program Files/Apache Group/Apache/htdocs', true); // move a directory and all subdirectories and files (recursive) // void dirmv( str 'source directory', str 'destination directory' [, bool 'overwrite existing files' [, str 'location within the directory (for recurse)']] ) function dirmv($source, $dest, $overwrite = false, $funcloc = NULL){ if(is_null($funcloc)){ $dest .= '/' . strrev(substr(strrev($source), 0, strpos(strrev($source), '/'))); $funcloc = '/'; } if(!is_dir(loc1 . $dest . $funcloc)) mkdir(loc1 . $dest . $funcloc); // make subdirectory before subdirectory is copied if($handle = opendir(loc1 . $source . $funcloc)){ // if the folder exploration is sucsessful, continue while(false !== ($file = readdir($handle))){ // as long as storing the next file to $file is successful, continue if($file != '.' && $file != '..'){ $path = $source . $funcloc . $file; $path2 = $dest . $funcloc . $file; if(is_file(loc1 . $path)){ if(!is_file(loc1 . $path2)){ if(!@rename(loc1 . $path, loc1 . $path2)){ echo '<font color="red">File ('.$path.') could not be moved, likely a permissions problem.</font>'; } } elseif($overwrite){ if(!@unlink(loc1 . $path2)){ echo 'Unable to overwrite file ("'.$path2.'"), likely to be a permissions problem.'; } else if(!@rename(loc1 . $path, loc1 . $path2)){ echo '<font color="red">File ('.$path.') could not be moved while overwritting, likely a permissions problem.</font>'; } } } elseif(is_dir(loc1 . $path)){ dirmv($source, $dest, $overwrite, $funcloc . $file . '/'); //recurse! rmdir(loc1 . $path); } } } closedir($handle); } } // end of dirmv() ?> This new function will be in 0.9.7 (the next release of File Manage) which should release sometime early February. Hope this helps some people. ---- Server IP: 216.194.113.175 Probable Submitter: 67.181.154.141 ---- Manual Page -- http://www.php.net/manual/en/function.rename.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+61152 Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+61152&report=yes&reason=added+to+the+manual Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+61152&report=yes&reason=bad+code Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+61152&report=yes&reason=spam Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+61152&report=yes&reason=useless Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+61152&report=yes&reason=non-english Delete: already in docs -- http://master.php.net/manage/user-notes.php?action=delete+61152&report=yes&reason=already+in+docs Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+61152&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+61152&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#102514) next »