Mark Frankli Mark Frankli - 5 months ago 21
PHP Question

How to rename a folder with content in it? - PHP

I want to rename a folder with files in it but it doesn't work. In the database it's successfully changed, but the folder didn't get the new name.


if (file_exists("user/$log_username")) {
rename("user/$log_username", "user/$un");

In the folder there're some photos and files, but I suppose the problem is that I want to rename the folder when it's running on the server. The user is logged in, when I want to rename his/her folder, but if I first log out the user the
will be equal to nothing. Is there any solution?

Answer Source

Copy folder to new name and delete the old folder when the user has logged of to make sure it all works as it's supposed to while user is in.
Or possibly when session ends of users can stay logged in for ever. Choose what is appropriate.

// Copy the folder
copy("user/$log_username", "user/$un");

 //Update database
 //Some code here

 // When user logs out