I have a script that when processed can either delete a huge amount of files from a given path on my server (200+), or transfer a huge amount of files from one directory into another directory, deleting the files from the first directory.
Both processes use PHP file-system functions, and are of the following functions I have defined:
PHP Code:Function for deleting a directory, and all its files underneath (since rmdir() wont delete directories with files):
function deldir($dir){
$current_dir = opendir($dir);
while($entryname = readdir($current_dir)){
if(is_dir("$dir/$entryname") and ($entryname != "." and $entryname!="..")){
deldir("${dir}/${entryname}");
}elseif($entryname != "." and $entryname!=".."){
unlink("${dir}/${entryname}");
}
}
closedir($current_dir);
rmdir(${dir});
}
All three functions when called will perform their task as needed, but the problem is that I need to do these processes with 100+ files, and I've read numerous places that running lengthy scripts like this probably isn't the best method through a browser.PHP Code:The two recursive functions for moving files to another directory:
function dotransfer($firstpath, $secondpath)
{
if(is_file($firstpath)){
$perms = fileperms($firstpath);
return rename($firstpath, $secondpath) && chmod($secondpath, $perms);
}
else if(is_dir($firstpath)){
dodirtransfer($firstpath, $secondpath);
}
else{
die("Cannot copy file: $firstpath (it's neither a file nor a directory)");
}
}
function dodirtransfer($firstpath, $secondpath)
{
$dir = opendir($firstpath);
while($file = readdir($dir)){
if($file == "." || $file == ".."){
continue;
}
dotransfer("$firstpath/$file", "$secondpath/$file");
}
closedir($dir);
}
The other method is using exec() commands, which I have very vague knowledge on how to use. I don't own my own web server, therefore there are a lot of shell commands I'm unfamiliar with how to use.
From PHP.net I found that the following:
Is suppose to delete a directory along with all the contents.PHP Code:exec("rm -rf /path/to/directory");
This would solve my problem for the first function I have that deletes a directory, but I'm left with how to use exec() to transfer files from one location to another, essentially with how I was doing it with rename() and the recursion.
----------
Any thoughts on a possible fix for my issue overall? I'm not sure if the method I'm doing it by with PHP file-system functions is all that worse off.




so be better to make sure the area of site is password protected, etc.

Bookmarks