PHP-Renaming Files and Directories


Renaming  Files and Directories

There are a few useful file and directory functions built-in to PHP which I'll cover in brief here. They include renaming and deleting files as well as listing files located in a directory. I'll review the syntax for some of them first and then demonstrate how they function within the context of a very useful PHP script.
First, there is the rename() function, which works as you would expect it to:
 
rename ("filenameold", "filnamenew");
 
It can be used on either files or directories.

Renames a file from the old filename to the new filename. The user that PHP runs as must have sufficient permissions to rename the file.


Returns TRUE on success; 
FALSE on failure

<?php

$fileold = "oldfile"; 
$filenew = "newfile"; 

if(!rename($fileold, $filenew)) 
{
    echo ("Rename process failed"); 
} 
 ?>