PHP File Functions
PHP have plenty of other functions related to file operations. below section will describe some of important file functions.
Copy File Function
PHP Provides copy function which will move files from one directory to another and etc. copy function can also rename the file.
<?php
//Copy file
copy("file.txt","data/file1.txt") or die("File not found");
?>
File Exists Function Example
File Exists function provide the functionality to check whether files exists or not before performing any operation on file.
<?php
//Check File Exists or not ?
if(file_exists("file.txt")){
echo 'file exists';
}else{
echo 'file does not exists';
}
?>
File Move/Rename Function Example
PHP Copy function also provide the functionality to rename the file but the proper method to rename/move the file is rename function.
<?php
//Move or Rename the file
rename("file.txt","data/file1.txt") or die("File not found");
?>
File Delete Function
PHP provides the delete function to remove the file during runtime.
<?php
//Deleting the file
if(unlink("file.txt")){
echo "File has been deleted";
}else{
echo "unable to delete file";
}
?>