- 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 exit()
- PHP exit() vs. break
- PHP isset()
- 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 mysqli Tutorial
- PHP mysqli Tutorial
- PHP and MySQL Setup
- PHP mysqli: Create Database
- PHP mysqli: Create Table
- 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 Functions
- PHP mysqli_connect()
- PHP mysqli_close()
- PHP mysqli_connect_errno()
- PHP mysqli_connect_error()
- PHP mysqli_query()
- PHP mysqli_fetch_row()
- PHP mysqli_fetch_assoc()
- PHP mysqli_fetch_array()
- PHP mysqli_free_result()
- PHP mysqli_error()
- PHP mysqli_prepare()
- PHP mysqli_stmt_bind_param()
- PHP mysqli_stmt_execute()
- PHP mysqli_stmt_fetch()
- PHP mysqli_stmt_store_result()
- PHP mysqli_stmt_num_rows()
- PHP mysqli_stmt_bind_result()
- PHP mysqli_stmt_get_result()
- PHP mysqli_result class
- PHP mysqli_report()
- PHP error_reporting()
- PHP mysqli_real_escape_string()
- PHP htmlspecialchars()
- PHP Misc Topics
- PHP Object Oriented
- PHP new Keyword
- PHP header()
- PHP getallheaders()
- 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 »