php move, copy and delete files
When we deal with files every day, we can delete files, rename files, or copy files.
In this section, we will explain some of the most commonly used operations in our daily lives.
Let’s talk about renaming first. The renaming function is:
Rename file
bool rename($old name,$new name);
This function returns a bool value to change the old name to the new name.
<?php //旧文件名 $filename = 'test.txt'; //新文件名 $filename2 = $filename . '.old'; //复制文件 rename($filename, $filename2); ?>
We open the directory and we can see the effect. You will find that the specified file is copied to the target path.
Copy files
Copying files is equivalent to cloning technology, cloning an original thing into a new thing. The two look exactly the same.
bool copy(source file, target file)
Function: Copy the source file with the specified path to the location of the target file.
Let’s play it through experiments and code:
<?php //旧文件名 $filename = 'copy.txt'; //新文件名 $filename2 = $filename . '_new'; //修改名字。 copy($filename, $filename2); ?>
Summary:
You will find that there is an extra file through the above example.
Delete file
Deleting a file means deleting a file in the specified path, but this deletion is direct deletion. If you are using a Windows computer, you cannot see this file in the Recycle Bin.
You will only find that this file has disappeared.
bool unlink(file with specified path)
<?php $filename = 'test.txt'; if (unlink($filename)) { echo "删除文件成功 $filename!\n"; } else { echo "删除 $filename 失败!\n"; } ?>