for example:
old name: records.zip
new name: company.zip

what if company.zip already exists? Will renaming the file will be aborted or instead records.zip will just be rename to company(2).zip?

Thanks

    I believe it will clobber the existing file, but you can always add some code to check for it first, then either abort the rename or unlink() the existing file first, just to be sure.

      NogDog;10944500 wrote:

      I believe it will clobber the existing file,
      but you can always add some code to check for it first, ...

      rename($oldname, $newname);

      Yes, [man]rename/man will overwrite the 'newname' if already is such a file
      so the existing file with that name will be totally lost

      This script will
      1. test if file to rename exists
      2. test if there is already a file with the new name
      ... suggest another name
      3. rename(old, new)
      4. tell if rename was a success or failed

      <?php
      
      $oldname = 'test1.txt';
      $newname = 'test2.txt';
      
      if(!file_exists($oldname)){
      	echo '"'.$oldname.'" no such file';
      }elseif(file_exists($newname)){
      	echo '"'.$newname.'" already exists';
      	echo '<br>try newname: "xyz_'.$newname.'"';//make up some other unique name
      }elseif(rename($oldname, $newname)){
      	echo '"'.$oldname.'" was renamed to "'.$newname.'"';
      }else{
      	echo '"'.$oldname.'" rename to "'.$newname.'" failed';
      }
      
      ?>
        Write a Reply...