note 92690 added to function.rename

From: Date: Tue, 04 Aug 2009 11:59:55 +0000
Subject: note 92690 added to function.rename
Groups: php.notes 
Request: Send a blank email to php-notes+get-159077@lists.php.net to get a copy of this message
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); ?> ---- Server IP: 213.249.64.162 Probable Submitter: 87.213.0.205 ---- Manual Page -- http://www.php.net/manual/en/function.rename.php Edit -- https://master.php.net/note/edit/92690 Del: integrated -- https://master.php.net/note/delete/92690/integrated Del: useless -- https://master.php.net/note/delete/92690/useless Del: bad code -- https://master.php.net/note/delete/92690/bad+code Del: spam -- https://master.php.net/note/delete/92690/spam Del: non-english -- https://master.php.net/note/delete/92690/non-english Del: in docs -- https://master.php.net/note/delete/92690/in+docs Del: other reasons-- https://master.php.net/note/delete/92690 Reject -- https://master.php.net/note/reject/92690 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#159077) next »