Jump to content

Need help creating array to copy file into hundreds of folders with different us


DiscoTrio

Recommended Posts

This will be a huge job and I could get it done by hand but we will need to do this many times.  Currently we have a system that creates folders and copies files into them.  The folders are named the same as the user accounts.  I have a database with all user accounts already in place.

 

I need help bouncing ideas on how to go bout making a file that would use those resources to re-copy the file into all those pre-existing folders so our old users don't miss out.

 

Is this enough info?  If it wont be to hard please include some coding.  Im using php (of course)

Link to comment
Share on other sites

More details:

When users sign up on my site they are given a folder and a file.

The file that is copied in is changed due to updates often.

We now have hundreds of users and when we change the file only new users get it becuase its to painfull to update file in every folder by hand...

We need a way to update all those folders automatically.

(All usernames stored in database which is the same as filename)

(text files are placed in all folders that have the username(filename) in it)

Link to comment
Share on other sites

Something like this would probably work. This is not tested and may have errors also I don't know what you're using to grab the files from one directory multiple directories?

 

<?php

// master directory with common files desired to populate user directories
$masterDir = '/somedirectory';

$masterFiles = scandir($masterDir);

//assuming all user directories are subdirectories of a higher level user directory
$userDir = '/userdirectory';

$userFiles = scandir($userDir);

for($i=0; $i<count($userFiles); $i++)
{
for($x=0; $x<count($masterFiles); $x++)
{
	copy($masterFiles[x], $userFiles[i]."/$masterFiles[x]");
}
}

?>

Link to comment
Share on other sites

ooo lookie here

 

<?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;
    }
?> 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.