I have a site with many files and directories. The directories are several levels deep. The problem 开发者_StackOverflow中文版is that they are in the wrong directory. They are in /www/upload/site/
and everything should be moved to /www/
. Is there a short script to move everything two directories up?
Thank you.
As noted by other answerer, PHP isn't the most elegant solution for this and it'd be far easier to just download an FTP program to move the files with. However, if you do want to implement this using PHP, you could write something like:
$dir=opendir('/www/upload/site/');
while ($file = readdir($dir)) {
if ($file == '.' || $file == '..') continue; // skip references to self
rename('/www/upload/site/'.$file, '/www/'.$file);
}
closedir($dir);
This process is inefficient and is brought about as easier methods such as using the Rename Function or Copy Function won't work in your case. Rename would require the directory you're 'renaming to' to be empty which it isn't as you're moving from within it, copy cannot copy complete directories. Therefore, we are left with the process or looping through each file and moving it to a new location manually using PHP's rename function on each individual item.
So whilst this process is certainly not one which PHP is necessary for, it's perfectly possible to do it and it's always useful to have the know how. It'd be worth reading up on PHP's File System Functions for more information on working with files/dirs.
You don't need a script. Get a good ftp program, such as FileZilla or FireFTP (needs firefox), and you could simply select all, and drag all folders and files in www/upload/
to www/upload
, and the ftp program will move them for you.
I don´t know how the server is set up, but probably this will not work as the user running the php script is not likely to have permissions to move files that you uploaded (unless they were uploaded using a php script as well...).
You could of course try something like:
<?php
exec('mv /www/upload/site/* /www');
?>
but I would be very careful with that.
精彩评论