Ajay
Ajay

Reputation: 370

How to move one directory to another directory?

I have two folders

  1. myappdemo.com/VueGuides/services/iclean

  2. myappdemo.com/VueGuides/services/pics

I need to move iclean folder into pics folder using PHP.

Upvotes: 16

Views: 46044

Answers (4)

Antony
Antony

Reputation: 4310

I required a different solution in my case as I was moving a sub folders contents into the parent folder. rename wouldn't work in my instance because the path was the same.

(For Linux based machines):

exec('mv '.$this->useFolder.'/'.$sub_folder.'/*'.' '.$this->useFolder);

This uses the inbuilt mv function through exec.

Upvotes: 1

Michael Berkowski
Michael Berkowski

Reputation: 270609

Use rename(). Note that if this runs on a web server, the web server user must have access to write to the target directory.

rename("oldpath", "newpath");

// in your case, assuming the script calling rename() 
// resides in the directory above 'myappdemo.com'
rename("myappdemo.com/VueGuides/services/iclean", "myappdemo.com/VueGuides/services/pics/iclean");

// Or use full absolute paths
rename("/path/myappdemo.com/VueGuides/services/iclean", "/path/myappdemo.com/VueGuides/services/pics/iclean");

Upvotes: 42

josegil
josegil

Reputation: 365

If you are worried about SEO i recommend you using redirects 301 in your .htaccess.

That must be something like that:

RewriteRule ^/VueGuides/services/iclean  http://myappdemo.com/VueGuides/services/pics  [NS,R=301,L]

Upvotes: 2

spider
spider

Reputation: 1178

There's a specific PHP function for it

http://php.net/manual/en/function.rename.php

Upvotes: 2

Related Questions