Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

jeimz173's avatar

How to rename a Folder with php

hi i am having trouble in renaming some folders in php. i tried rename() but it didnt worked.. can anyone help me thank you :)

0 likes
7 replies
Sirik's avatar

Maybe you're not the owner of that folder

1 like
mdecooman's avatar

Hi @jeimz173

Maybe you should check the doc related to file management first here and come back to the forum with a more precise question. Happy to help if you have further roadblocks along the way.

Are you using an IDE? It will help you to dig into the code base.

Good hunting :)

harran's avatar

probably your code doesnt have the write permission

just give the directory full permission with this command

sudo chmod 777 directory/name
jeimz173's avatar

@mdecooman im sorry if my question is not precise and being a newbie here...mmm how bout this, i have a function that updates the name of my category and at the same time it updates also my folder name. but the problem is i dont have any idea how to update my folder name at the same time.. and thats it i tried to use the rename() but failed...

mdecooman's avatar

Hi @jeimz173

Don't be sorry to be a newbie... We all are newbies but at different levels.

Ok, there are different ways to do things:

  1. When looking a bit more in the doc and tinkering a little with the code, you can see that Laravel uses the rename function of PHP. Like in the class vendor/laravel/framework/src/illuminate/Filesystem/Filesystem.php. It has a function move and another moveDirectory, both are wrappers around the PHP rename function. Therefore... you can rename a file or folder.
  2. You can think about something else like tagging your folders and changing the tags accordingly instead of touching the file structure.
  3. etc.

Hope it helps...

1 like
jeimz173's avatar

hi @mdecooman thanks for your suggestion but i tried another way and it worked for me :)

what i did is add new column to my migration and store the path to my database then i used random() string for the name of my folder so, i don't need to rename it anymore at the same time whenever i update my category

Please or to participate in this conversation.