Why is PHP rename($file) still deleting files?

Your main problem is, that your source and destination directory is the same and the names of the old files and new files are overlapping.

Looking at the first two lines of your output should show you the problem. The newly created file list1.txt is deleted in the second loop iteration.

A simple, but working solution for your problem should be to create a new directory where you move and rename all files (rename should work for this) and later delete the now empty source directory and replace it with the temporary one.