Reputation: 51
I am using the following script to rename images in a folder. When executed the first 2 (new) names are being failed, so it basically starts with 3.
function getFileExtension($filename){
return substr($filename, strrpos($filename, '.'));
}
$counter = 1;
if ($handle = opendir('images')) {
while (false !== ($fileName = readdir($handle))) {
$newCounter = str_pad($counter, 3, '0', STR_PAD_LEFT);
$prefix = '11225';
$newName = $prefix.$newCounter;
rename('images/'.$fileName, 'images/'.$newName.getFileExtension($fileName));
$counter++;
}
closedir($handle);
}
These are the warnings I get,
Warning: rename(images/.,images/11225001.): The process cannot access the file because it is being used by another process. (code: 32) in C:\wamp\apache2\htdocs\renameimages\index.php on line 21
Warning: rename(images/..,images/11225002.): Access is denied. (code: 5) in C:\wamp\apache2\htdocs\renameimages\index.php on line 21
Is there anything I am missing?
Upvotes: 1
Views: 156
Reputation: 439
First two is links to current folder and parent folder. You should skip them.
if($fileName=='.' || $filename=='..')continue;
Upvotes: 1