note 92690 deleted from function.rename by danbrown

From: Date: Tue, 04 Aug 2009 17:08:36 +0000
Subject: note 92690 deleted from function.rename by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-159095@lists.php.net to get a copy of this message
Note Submitter: kander at gmail dot com ---- Re: Sudhir's example - here is a reworked version using the DirectoryIterator. Three advantages: 1. OS-agnostic - works on both Windows and Linux. 2. Can easily be modified to recurse into subdirectories (just replace the DirectoryIterator with RecursiveDirectoryIterator) 3. Faster (0.44 seconds vs 0.52 seconds, for 10.000 files) And, arguably, cleaner code. <?php $path = "/home/sander/test"; // Use RecursiveDirectoryIterator if you want to rename within within subfolders. $directory = new DirectoryIterator($path); foreach($directory as $dirElement) { if($dirElement->isFile() && substr($dirElement->getBasename(),-4) === '.mp3') { $newName = substr($dirElement->getPathname(), 0, -3).'_mysongs_mp3'; echo '<br />'.$newName; rename($dirElement->getPathname(), $newName); } } unset($directory); ?>

« previous php.notes (#159095) next »