1

I have an HTML form and one of the inputs creates a folder. The folder name is chosen by the website visitor. Every visitor creates his own folder on my website so they are randomly generated. They are created using PHP Code.

Now I would like to write a PHP code to copy a file to all of the child directories regardless the quantity of directories being generated.

I do not wish to stay writing a PHP line for every directory that is created - i.e. inserting the filename name manually (e.g. folder01, xyzfolder, folderabc, etc...) but rather automatically.

I Googled but I was unsuccessful. Is this possible? If yes, how can I go about it?

Kindly ignore security, etc... I am testing it internally prior to rolling out on a larger scale.

Thank you

user2333968
  • 135
  • 1
  • 3
  • 13
  • You mean you need to add a file to all the existing folders? Assuming all the folders share a common parent path use [DirectoryIterator](http://php.net/manual/en/class.directoryiterator.php) to iterate (loop) over all the folders and perform the desired operation. Or do you mean when creating a the new folder you magically want a file added to the newly created folder? – ficuscr Jun 24 '13 at 01:08
  • I'd like the users to create the directory. Then whenever I want to push a new file to their directory. I can run a specific command to copy this new file to their directory regardless of the name and quantity of the folder. – user2333968 Jun 24 '13 at 01:13
  • Yup. I'd start with looking at SPL's DirectoryIterator. Here are some examples of code looping over directories: http://stackoverflow.com/questions/2014474/php-read-sub-directories-and-loop-through-files-how-to – ficuscr Jun 24 '13 at 01:20
  • @ficuscr Thanks for your help. I'll give it a look tomorrow :-) – user2333968 Jun 24 '13 at 01:40
  • `Kindly ignore security, etc... I am testing it internally prior to rolling out on a larger scale.` - famous last words. – Burhan Khalid Jun 24 '13 at 03:51
  • Lol I know. Most of the time when I post the question, I get many comments not to ignore security. I know it provokes laughter but I felt I had to do it :-) – user2333968 Jun 24 '13 at 19:58

1 Answers1

0

It is sad I cannot comment so go on...

//get the new folder name
$newfolder = $_POST['newfoldername'];
//create it if not exist
if(!is_dir("./$newfolder")) {
  mkdir("./$newfolder", 0777, true);
}


//list all folder
$dirname = './';
$dir = opendir($dirname); 
while($file = readdir($dir)) {
    if(($file != '.' OR $file != '..') AND is_dir($dirname.$file))
    {
        //generate a randomname
        $str = 'yourmotherisveryniceABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
        $randomname = str_shuffle($str);
        $actualdir = $dirname.$file;
            //copy of the file
            copy($uploadedfile['tmp_name'],  $actualdir.$randomname);

    }
}
closedir($dir);

I just want to say, you seem to be lazy by looking for what you want to do. because when I read "I would like to write a PHP code to copy" the answer is in your sentence: copy PHP and list of folders regarless how many? Then just simply list it !

Maybe you need to learn how to use google... If you search "I would like to write a PHP code to copy a file to all of the child directories regardless the quantity of directories being generated" Sure you will never find.

Dorian_gd
  • 181
  • 1
  • 13
  • Hi, your comment made me laugh. I tried your code and it gave me the following errors in the error long: `mkdir() [function.mkdir]: File exists in {path} on line 6 copy() [function.copy]: Filename cannot be empty in {path} on line 20 ` – user2333968 Jun 24 '13 at 19:49
  • Hello. It is because if you try to create a folder already exist, it return this warking. I just added a condition to test it. `if(!is_dir())` – Dorian_gd Jun 25 '13 at 00:18