16

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.

1
  • is this an one off problem you have? Or you are going to do it regularly as part of a web application. If you are just looking for a file manager written in PHP you can use: pfn.sourceforge.net
    – nwaltham
    Dec 9, 2011 at 13:40

4 Answers 4

42

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");
4
  • hi put code i get Warning: rename(www.myappdemo.com/VueGuides/services/iclean,www.myappdemo.com/VueGuides/services/pics) [function.rename]: No such file or directory in /home/myappdemo/myappdemo.com/VueGuides/services/getZip.php on line 55
    – Ajay
    Dec 9, 2011 at 13:59
  • 1
    @user816891 Clearly you've used the wrong path name. Your path is /home/myappdemo/myappdemo.com/... and you have specified www.myappdemo.com/.... You need to use the exact correct file path. That's a file path, not a URL path... Dec 9, 2011 at 14:01
  • doesn't the example above RENAME the dir 'iclean' to 'pics' (replacing the 'pics' dir completely)? The task is to move 'iclean' inside 'pics', so should it not be rename("myappdemo.com/VueGuides/services/iclean", "myappdemo.com/VueGuides/services/pics/iclean"); ? Oct 8, 2013 at 22:36
  • @mwb Yes it should have been. On the Unix command line, a mv would have placed it into the target directory which already existed. PHP rename() would indeed replace it but fail if the target directory was not empty. Oct 8, 2013 at 23:14
2

There's a specific PHP function for it

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

2

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]
1

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.