How to copy and rename a file in php - A file can of course be copied, renamed, and deleted. In the php programming language also provides copy command, rename command and delete command. To run the command in php must have functions. The following functions are used.
Program
File Name: file13.php
Description: Program to copy, rename and delete file.
Type the following php code into notepad.
<?php
$file = "data.txt";
$newfile = "newdata.txt";
$newfile2 = "newdata2.txt";
if (copy ($file, $newfile)) {
echo "File <b>$file</b> Successfully copied into <b>$newfile</b>. <br>";
}
if (rename ($newfile, $newfile2)) {
echo "File <b>$newfile</b> Successfully copied into <b>$newfile2</b>. <br>";
}
if (unlink ($newfile2)) {
echo "File <b>$newfile2</b> Successfully deleted. <br>";
}
?>
Save the php code with file13.php name in htdocs folder.
Program Explanation
The program above there are several functions to copy, rename and delete files. The copy() function on line 5 will duplicate the $file file to the $newfile file. Next rename() will rename file $newfile to $newfile2. The unlink() function on row 11 will delete the file $newfile2.
To run the program, open the browser type http://localhost/file13.php in the address bar then enter. The result will look like this.
That's my tutorial on how to copy and rename a file in php. Hope can be useful for all of us. Learn also: How to Get File Information in PHP.
- Function copy() to copy the file.
- Function rename() to rename the file.
- Function unlink() to delete the file.
Program
File Name: file13.php
Description: Program to copy, rename and delete file.
Type the following php code into notepad.
<?php
$file = "data.txt";
$newfile = "newdata.txt";
$newfile2 = "newdata2.txt";
if (copy ($file, $newfile)) {
echo "File <b>$file</b> Successfully copied into <b>$newfile</b>. <br>";
}
if (rename ($newfile, $newfile2)) {
echo "File <b>$newfile</b> Successfully copied into <b>$newfile2</b>. <br>";
}
if (unlink ($newfile2)) {
echo "File <b>$newfile2</b> Successfully deleted. <br>";
}
?>
Save the php code with file13.php name in htdocs folder.
Program Explanation
The program above there are several functions to copy, rename and delete files. The copy() function on line 5 will duplicate the $file file to the $newfile file. Next rename() will rename file $newfile to $newfile2. The unlink() function on row 11 will delete the file $newfile2.
To run the program, open the browser type http://localhost/file13.php in the address bar then enter. The result will look like this.
Program view |
That's my tutorial on how to copy and rename a file in php. Hope can be useful for all of us. Learn also: How to Get File Information in PHP.
Comments
Post a Comment