- PHP Basics
- Learn PHP
- PHP Comments
- PHP Data Types
- PHP Variables
- PHP Operators
- PHP echo
- PHP print
- PHP echo vs. print
- PHP if else
- PHP switch
- PHP for Loop
- PHP while Loop
- PHP do...while Loop
- PHP foreach Loop
- PHP break and continue
- PHP Arrays
- PHP print_r()
- PHP unset()
- PHP Strings
- PHP Functions
- PHP File Handling
- PHP File Handling
- PHP Open File
- PHP Create a File
- PHP Write to File
- PHP Read File
- PHP feof()
- PHP fgetc()
- PHP fgets()
- PHP Close File
- PHP Delete File
- PHP Append to File
- PHP Copy File
- PHP file_get_contents()
- PHP file_put_contents()
- PHP file_exists()
- PHP filesize()
- PHP Rename File
- PHP fseek()
- PHP ftell()
- PHP rewind()
- PHP disk_free_space()
- PHP disk_total_space()
- PHP Create Directory
- PHP Remove Directory
- PHP Get Files/Directories
- PHP Get filename
- PHP Get Path
- PHP filemtime()
- PHP file()
- PHP include()
- PHP require()
- PHP include() vs. require()
- PHP and MySQLi
- PHP and MySQLi
- PHP MySQLi Setup
- PHP MySQLi Create DB
- PHP MySQLi Create Table
- PHP MySQLi Connect to DB
- PHP MySQLi Insert Record
- PHP MySQLi Update Record
- PHP MySQLi Fetch Record
- PHP MySQLi Delete Record
- PHP MySQLi SignUp Page
- PHP MySQLi LogIn Page
- PHP MySQLi Store User Data
- PHP MySQLi Close Connection
- PHP Misc Topics
- PHP Object Oriented
- PHP new Keyword
- PHP Cookies
- PHP Sessions
- PHP Date and Time
- PHP GET vs. POST
- PHP File Upload
- PHP Image Processing
PHP rename() | Rename a File or Directory
The PHP rename() function is used when we need to change the name of a file or a directory. For example:
<?php $chk = rename("myfile.txt", "yourfile.txt"); if($chk) echo "<p>Name changed successfully!</p>"; else echo "<p>Unable to change the name.</p>"; ?>
The output of the above PHP example using the rename() function is shown in the snapshot given below:
That is, after executing the above PHP code, the name of the file myfile.txt gets changed to yourfile.txt.
PHP rename() Syntax
The syntax of the rename() function in PHP is:
rename(oldName, newName, context)
The first two (oldName and newName) parameters are required, whereas the last parameter is optional and is used to specify the context resource while renaming a file or a directory.
PHP rename() function example
Consider the following PHP code as another example of the rename() function:
PHP Code
<?php $old_file_name = "old_file.txt"; $new_file_name = "new_file.txt"; if (file_exists($old_file_name)) { if (rename($old_file_name, $new_file_name)) { echo "File renamed successfully."; } else { echo "File renaming failed."; } } else { echo "File does not exist."; } ?>
Output
File renamed successfully.
And if the file "old_file.txt" does not exist in the current directory, then you will get the following output:
File does not exist.
Advantages of the rename() function in PHP
- Only a few lines of code are needed to rename a file or directory when using the rename() function.
- An easy and quick way to rename a file or directory on a server is to use the rename() function.
- The function outputs a boolean value that can be used to determine whether or not a file or directory was successfully renamed. This makes handling errors very simple.
Disadvantages of the rename() function in PHP
- The rename() function can introduce security flaws like directory traversal attacks if it is used without proper input validation.
- The rename() function cannot be easily undone once a file or directory has been renamed. This implies that before renaming something, it is crucial to give it careful thought.
- Only files and directories can be renamed using the rename() function. Use other functions or libraries if you need to perform more complicated operations.
« Previous Tutorial Next Tutorial »