We help IT Professionals succeed at work.

Php, move folders using rename function

Pedro Chagas
Pedro Chagas asked
Medium Priority
Last Modified: 2012-05-12
I just want to do a simple question: when we use function "rename" to make a move of folder, what happening to the files of that folder, is also move at the same time?

The best regards, JC
Watch Question

Everything stays right where it is.  Nothing actually moves.

Let's pretend you have children and grandchildren (which forms a family tree not that unlike a directory tree).  If we were to go to court and legally change your name, what happens to them?  (Nothing.)
Hi JC,

Apologies as I haven't tested it myself but i believe if you use the rename() function to 'move' a folder as  you have stated, the files within that folder will also be moved.  Essentially when using rename() you are saying I want to change the name of the folder, the contents within the folder should remain the same but be moved to the new folder.

Best advice would be to try it and see.

On a side note, I would expect that permissions on files/folders within the folder you are renaming should stay the same as well but I'm not sure.


Having said that, if you do a mv operating in Linux, files will move if you change file systems.  But you asked a PHP question and not a Linux question.  (In Linux, if you stay on the same file system, my above answer works except your children might end up with different grandparents...)
Yes, the files stay in the folder, it's identical to the shell command "mv" - see demonstration below.
[xterm@foo ee]$ cat rename.php
<?php rename("foo","bar"); ?>

[xterm@foo ee]$ ls -l foo/
total 0
-rw-r--r-- 1 xterm users 0 Nov  3 10:20 hello

[xterm@foo ee]$ php -q rename.php 

[xterm@foo ee]$ ls -l bar/
total 0
-rw-r--r-- 1 colinb users 0 Nov  3 10:20 hello

Open in new window

Pedro ChagasWebmaster


Hi @xterm, what kind of code you write?
That is all Php?

Regards, JC


No, that is all from a Unix bash command prompt.  The only PHP code is in between the <?php ?> tags.

I was just demonstrating that using the PHP rename() function, the folder foo became folder bar, and retained the file "hello" inside it after the rename.

Explore More ContentExplore courses, solutions, and other research materials related to this topic.