Change language

PHP rename function ()

|
Syntax:
rename (oldname, newname, context)
Parameters used:
The rename() function in PHP takes three parameters.
  • oldname:This is a required parameter and specifies the old file or directory name.
  • newname:is a required parameter and specifies the new name of a file or directory.
  • context:is an optional parameter that determines the behavior of the stream.
  • Returned value:
    Returns True on success and False on failure.Errors and exceptions
  • The rename() function generates a warning if a new name already exists when renaming a directory.
  • The wrapper used in oldname must match the wrapper used in newname.
  • If the target filesystem does not allow system calls chown() or chmod ( ) make files, the rename() function can generate warnings.
  • Examples:
    Input: $old_name = "gfg.txt"; $new_name = "newgfg.txt"; rename ($new_name, $old_name); Output: 1 Input: $old_name = "gfg.txt"; $new_name = "newgfg.txt"; if (file_exists ($new_name)) {echo "Error While Renaming $old_name"; } else {if (rename ($old_name, $new_name)) {echo "Successfully Renamed $old_name to $new_name"; } else {echo "A File With The Same Name Already Exists"; }} Output: Successfully Renamed gfg.txt to newgfg.txt 
    The programs below illustrate the rename() function.Suppose there is a file named "gfg.txt"Program 1
    // Old file name $old_name = "gfg.txt" ;  
    // New filename $new_name = "newgfg.txt" ;  
    // using the rename() function to rename the file rename ( $old_name , $new_name );  
    ?>
    Output:
    1 
    Program 2 // Old file name $old_name = "gfg.txt" ;  
    // New filename $new_name = "newgfg.txt" ;  
    // Check if file already exists if ( file_exists ( $new_name )) { echo "Error While Renaming $old_name" ; } else { if (rename ( $old_name , $new_name )) { echo "Successfully Renamed $old_name to $new_name" ; } else { echo "A File With The Same Name Already Exists " ; } } ?>
    Output:
    Successfully Renamed gfg.txt to newgfg.txt 
    Related Articles:
    • PHP | copy() function
    • PHP | unlink() function
    Link:
    http://php.net/manual/en/function.rename.php

    PHP rename function () PHP: Questions

    PHP rename function () rename: Questions

    Shop

    Best laptop for Fortnite

    $

    Best laptop for Excel

    $

    Best laptop for Solidworks

    $

    Best laptop for Roblox

    $

    Best computer for crypto mining

    $

    Best laptop for Sims 4

    $

    Best laptop for Zoom

    $499

    Best laptop for Minecraft

    $590

    Latest questions

    NUMPYNUMPY

    psycopg2: insert multiple rows with one query

    12 answers

    NUMPYNUMPY

    How to convert Nonetype to int or string?

    12 answers

    NUMPYNUMPY

    How to specify multiple return types using type-hints

    12 answers

    NUMPYNUMPY

    Javascript Error: IPython is not defined in JupyterLab

    12 answers

    Wiki

    Python OpenCV | cv2.putText () method

    numpy.arctan2 () in Python

    Python | os.path.realpath () method

    Python OpenCV | cv2.circle () method

    Python OpenCV cv2.cvtColor () method

    Python - Move item to the end of the list

    time.perf_counter () function in Python

    Check if one list is a subset of another in Python

    Python os.path.join () method