Dynadot โ€” .com Transfer

PHP mkdir() with copy of pre-made files?

Spaceship Spaceship
Watch

Obulus

Established Member
Impact
0
Hey there,
Im building a small system and would really like your help.

I'm looking for a function whereby, upon user registration a directory is made with their userid and the files I already have in a seperate folder are copied into the new one that has just been made

I have the registration system built, it's just the copying of files into the directory that is produced.

It'd be easier for me to copy the entire directory as it can be a hefty task copying over 100 files into a new subdirectory in an array, something I simply do not have the time (or patience) for.

Kind Regrads and the Greatest Thanks for any help.
 
0
•••
The views expressed on this page by users and staff are their own, not those of NamePros.
GoDaddyGoDaddy
Found the following on the php.net manual:

PHP:
function smartCopy($source, $dest, $folderPermission=0755,$filePermission=0644){
# source=file & dest=dir => copy file from source-dir to dest-dir
# source=file & dest=file / not there yet => copy file from source-dir to dest and overwrite a file there, if present

# source=dir & dest=dir => copy all content from source to dir
# source=dir & dest not there yet => copy all content from source to a, yet to be created, dest-dir
    $result=false;
   
    if (is_file($source)) { # $source is file
        if(is_dir($dest)) { # $dest is folder
            if ($dest[strlen($dest)-1]!='/') # add '/' if necessary
                $__dest=$dest."/";
            $__dest .= basename($source);
            }
        else { # $dest is (new) filename
            $__dest=$dest;
            }
        $result=copy($source, $__dest);
        chmod($__dest,$filePermission);
        }
    elseif(is_dir($source)) { # $source is dir
        if(!is_dir($dest)) { # dest-dir not there yet, create it
            @mkdir($dest,$folderPermission);
            chmod($dest,$folderPermission);
            }
        if ($source[strlen($source)-1]!='/') # add '/' if necessary
            $source=$source."/";
        if ($dest[strlen($dest)-1]!='/') # add '/' if necessary
            $dest=$dest."/";

        # find all elements in $source
        $result = true; # in case this dir is empty it would otherwise return false
        $dirHandle=opendir($source);
        while($file=readdir($dirHandle)) { # note that $file can also be a folder
            if($file!="." && $file!="..") { # filter starting elements and pass the rest to this function again
#                echo "$source$file ||| $dest$file<br />\n";
                $result=smartCopy($source.$file, $dest.$file, $folderPermission, $filePermission);
                }
            }
        closedir($dirHandle);
        }
    else {
        $result=false;
        }
    return $result;
    }

So you could have your original directory and copy its contents recursively to another directory. The permissions parameters are the chmod permissions for the new files and directorys.
Hope that helps!
 
0
•••
Thank you so much! Really helped!
 
0
•••
Appraise.net
Domain Recover
DomainEasy โ€” Zero Commission
  • The sidebar remains visible by scrolling at a speed relative to the pageโ€™s height.
Back