You’ll need to keep a note of how many images you’ve already copied, and start the loop at the next one. Your code is fine, but each time you load it, it will try to copy the same two images that it’s already copied. So one easy way might be instead of refreshing, have the code create a form with a submit button that contains a hidden form variable you can use as the loop start point. When you press the button, it re-calls the same code, which can read the start-point from the form variables, add 2 to it, do the job, and re-draw the form with the new start value. Or you could stick it in a session variable.
I actually wrote a very similar thing, but I deleted it because, reading the doc for the PHP rename function, it does very much give the impression that it’s copying the file and deleting the original. Maybe it varies from OS to OS, but the fact it can rename from one disk partition to another suggests it is copying.
PHP is but a tiny wrapper around system calls or third-party apis. And for sure rename() doesn’t do anything of its own but calling an os routine. And every os renames a file by means of just rewriting an index table if happens within the same logical disc/partition or copy/erase otherwise.
Fair enough. It intrigued me, but not enough to start looking around to see if was easy to find source. It’s strange, though, that the manual page suggests that there was an enhancement to the php rename() function to support renaming between different disks, which if it only calls the underlying OS rename, would not be something that the php code has any influence on.