Write a php script to check and change file permissions copying renaming and deleting files

Sometimes we need to do some basic tasks in the project like rename, copy, delete and move the file from one location to another. In this post, we will try to explain, how you can Rename, Copy, and Delete the files in PHP.

PHP copying a file

To copy a file, we can use copy[] function in PHP. Let’s suppose we have one file with some content, we need to create another file somewhere in the project. Now the copy[] function will copy the content of the first file to another. Once the file is copied you will get true otherwise false, and accordingly, you can set the output message.

The following example shows how to copy the test1.txt file to test2.txt file.

Note: both the files are at the same location in the below script. You can change it and add the location with the file name.

PHP renaming a file

To rename a file, we can use rename[] function in PHP. This function rename the file with given name as in the below code.

For example, to rename the test1.txt file to test2.bak file, you use the following code:

copy[] function example

PHP rename[] Function

The rename[] function is used to rename a file name.

rename[] function syntax

rename[] function example

PHP unlink[] Function

How to Delete a file in PHP.

The unlink[] function is used to delete a file.

unlink[] function syntax

unlink[] function example

Chủ Đề