Encodera

Thanks for reading!

Donate to the site — we work hard for you.

PHP reference

rename

Renames or moves a file.

Syntax

rename(string $from, string $to): bool

Parameters

ParameterTypeDescription
$fromstringThe current path.
$tostringThe new path.

Return value

bool`true` if the operation succeeded.

Examples

Renaming a file:

<?php
 
$old = sys_get_temp_dir() . '/ref-old.txt';
$new = sys_get_temp_dir() . '/ref-new.txt';
file_put_contents($old, 'data');
 
rename($old, $new);
 
var_dump(file_exists($old));
var_dump(file_exists($new));

Output:

bool(false)
bool(true)

Notes

To move a file, give another folder in the new path. Files uploaded through a form are moved with move_uploaded_file(), not this function.

See also

Related recipes

In the tutorial

  • Reading and writing files — Working with files in PHP — writing, reading, appending, reading line by line, checking existence and deleting.

Thanks for reading!

Donate to the site — we work hard for you.