PHP Code:
if (file_exists($path))
        while (
// get file extension
$extension pathinfo($pathPATHINFO_EXTENSION);
// get file's name
$filename pathinfo($pathPATHINFO_FILENAME);
// add and combine the filename, iterator, extension
$new_filename $filename '-' $iterator '.' $extension;
// add file name to the end of the path to place it in the new directory; the while loop will check it again
$path $directory $new_filename;
This code is supposed to rename a file, if it exists, to the name of that file with a hypen and a number. Example: "word_document.doc" to "word_document-1.doc". And if word_document-1.doc exists, rename it to word_document-2.doc, and so on. Instead, if a file exists, I get word-document-1.doc (works the first time) but then it just adds the iterator instead of substituting it for files that follow with the same name. So I get word_document-1-2.doc, and word_document-1-2-3.doc. I think I can see why this is happening, the new file variable contains the previous while loop's iterator, but I don't know how to fix it without using some regex to detect the previous iterator, and I think that is probably not needed here. Please help.