Last Updated: May 31, 2021
·
26.02K
· simon_hen

Rename folder in PHP

Sometimes, for any reason, we need to rename folder's name in PHP (like I do for system folder in EE.).

There is a function in php called <a href="http://php.net/manual/en/function.rename.php">rename</a>. This function works perfectly with files.

The problems (in my case) begin with folder's rename, when I want to do it, I'm using :

rename("myoldfolder","mynewfolder");

Now,when I'm using this, I have the next error :

Warning: rename(/myoldfolder,/mynewfolder) [function.rename]: Permission denied in /Applications/MAMP/htdocs/config_dev/configure.php on line 26

This is not a real permission problem around here because I have the right to do it.

But, how ?

The idea is to give an absolute link and not a relative link like this :

rename(realpath(dirname(__FILE__)).'/myoldfolder',realpath(dirname(__FILE__)).'/mynewfolder');

And, voila ! Everything should works perfectly (like in my place) !

realpath(dirname(__FILE__))

This piece of code is going to give your current folder where the code is executed like this :

/Applications/MAMP/htdocs/current_folder/mynewfolder

Hope it helps some of you !

Simon

1 Response
Add your response

Hy
i am using this rename function with some editing like
saving folders name in variables
following is my code:

rename(realpath(dirname(FILE)).$oldfoldername,realpath(dirname(FILE)).$newfoldername);

$oldfoldername= $row['facility_name'];

old folder name is coming from D.B.

$newfoldername=$startback.$date.$endback.$oldfoldername

new folder name is a combination of date time and some alphabets.

i am getting following error :

Warning: rename(C:\xampp\htdocs\restaurantd,C:\xampp\htdocs\restaurantback%05/20/2014 08:21:34 %d) [function.rename]: The system cannot find the file specified. (code: 2) in C:\xampp\htdocs\restaurant\DeleteUser.php on line 31.

i need help.
thanx in advance :)

over 1 year ago ·