PHP File Rename/Delete

Rename A File :

The rename() function is used to rename a file in php. Example :
<?php

  $file = "file.txt";

  if(file_exists($file)){
    if(rename($file, "Newfile.txt")) {
      echo "File renamed successfully.";
    } else {
      echo "Error : File cannot be renamed.";
    }
  } else {
    echo "ERROR : File does not exist.";
  }
  
?>
Output :

File renamed successfully.


Delete A File :

The unlink() function is used to delete files in php. Example :
<?php

  $file = "file.txt";

  if(file_exists($file)){
    if(unlink($file)) {
      echo "File deleted successfully.";
    } else {
      echo "Error : File cannot be deleted.";
    }
  } else {
    echo "ERROR : File does not exist.";
  }
  
?>
Output :

File deleted successfully.

Next Topic :