Jump to content

[function.rename]: File exists


dubc07

Recommended Posts

Is there any way of overwriting a file name if it exists.

 

Basically i have a program which deletes a file name image3 or whatever, but i need to rename all images in the folder in line LIKE image1 image2 image3 image4 image5 but the files already exists.

 

this is the error i get

[function.rename]: File exists in C:\wamp\www\folder1\fin1_picedit.php on line 106

Link to comment
https://forums.phpfreaks.com/topic/155934-functionrename-file-exists/
Share on other sites

/**
* Renames a file or directory. If a file or directory called $newname exists, tries to unlink it first.
*/
function renameWithOverwrite($oldname, $newname, $context = null) {
  $outcome = true;
  if (file_exists($newname)) {
    if(!unlink($newname, $context)) $outcome = false;
  }
  if(!rename($oldname, $newname, $context)) $outcome = false;
  return $outcome;
}

 

not tested :P

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.