note 36068 added to function.rename
| From: | aly at nepadsn dot org | Date: | Wed, 24 Sep 2003 17:32:44 +0000 |
| Subject: | note 36068 added to function.rename | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-57065@lists.php.net to get a copy of this message | ||
I have a strange behavior appearing while renaming a big set of files (seems to appear when there
are more than 300 files in the directory, i am using a redhat 9.0 newly installed, php 4.2.2 with
apache 2) :
my feeling is that the handler $handler=$thisdir->read()
with $thisdir=dir($path)
is getting modified all along the process and the while($handler=$thisdir->read() {
... renaming
}
is somehow restarting from the begining at some point so that if i have 500 files in the directory
it will loop 570 times for instance, so that 70 files will have been renamed twice...
This happens only when the name itself changes (adding or removing some characters), it won't
happen if i just do a lowercase or uppercase in the rename function.
Note that i do this outside of the publishing directory (i took care of applying the proper rights
to the directories, recursively)
Here is the code (i am adding '_1' before the file extension) :
$dir="/backup/photos/photos_catalog/test";
$type="psd";
$x = 0;
//echo is_dir($dir);
if(is_dir($dir)) {
$thisdir = dir($dir);
while($entry=$thisdir->read()) {
if(($entry!='.')&&($entry!='..')) {
$isFile = is_file($dir . '/' . $entry);
$temp = explode(".", $entry);
$x++;
if (($isFile) && (strtolower($type) == strtolower($temp[count($temp) - 1])))
{
$old = $entry;
$new = $temp[0].'_1.'.$type;
echo $x . ' = ' . $old . ' -> '. $new . '<br>';
rename($dir.'/'.$old,$dir.'/'.$new);
}
}
}
}
----
Manual Page -- http://www.php.net/manual/en/function.rename.php
Edit Note -- http://master.php.net/manage/user-notes.php?action=edit+36068
Delete Note -- http://master.php.net/manage/user-notes.php?action=delete+36068&report=yes
Reject Note -- http://master.php.net/manage/user-notes.php?action=reject+36068&report=yes