pTutorial

New way to learn PHP function

PHP rename Function

Syntax

rename(existing_namem, new_name);
Parameter Description
existing_namem $string (file name that you want to rename)
new_name $string (new file name )

Definition

It returns true if file is renamed successfully otherwise return false.

Usage

Rename function is used to rename the file or directory with the existing name to the new name.

.

Note: File must be there otherwise warning message will displayed.

example of rename function

<?php

$a=@rename('abc.txt', 'xyz.php');
if($a)
{
echo " successfully renamed ";
}
else
{
echo "error occur";
}
?>

abc.txt is source file and xyz.txt is destination file for rename function. If file renamed successfully, successfully renamed message is displayed.

AT (@) sign is used to suppress/ignore the warning message.

Example

<?php
$a = @rename('abc.txt', 'php/xyz.txt');
if($a)
{
echo " successfully renamed ";
}
else
{
echo "error occur";
}
?>

Output

error occur

If you want to move the file into the sub directory used above syntax.

If you want to move file with new name in another directory, pass absolute or relative path as a second argument.

Example

<?php

$a = @rename('../php/abc.txt', 'xyz.php');
if($a)
{
echo " Successfully renamed ";
}
else
{
echo "Error occur";
}

?>

Output

Error occur

You can use absolute path as well as relative as shown above.

Note: Specified file or directory is not exist so that output will "error occur".

QUOTES OF THE DAY

Impossible is a word only be found in the dictionary of fools.

LIKE ON FACEBOOK

+1 ON GOOGLE

SHARE ON TWITTER