I have been looking and looking, but I can't find a php code that does this. What I am needing is a php script that will have a text filed with a button. The user will type in a name into the text field. When the user clicks the button, the script copies a pre defined directory and renames the dir with whatever the user input into the text field. Is this possible, and if so, please explain in detail. I am still learning php.

Thank you!

    speck2000:

    Get the user's input,

    and use something like

    	if (file_exists($dir) && is_dir($dir)) {
    		echo "Failed - Directory Already Exists";
    		return false;
    	} else {
    

    Then if you click here you will see the very first user comment has a function - how to copy directories.

    You can also use realpath() andgetcwd() to help you out.

    I would recommend getting the users input, and using the code above to see if the directory already exists (unless you already have something to check that).

    Once you are done use the code provided on php.net to copy the files over, and voila, your done.

    Good Luck.

      I am pretty new at this. Can someone maybe send me an example of code?

      Thanks!

        big.nerd did prove you some example code. Since you're only renaming, I'd rather do this with a system call from PHP than using PHP's copy functions, myself, but to each his own.

        if ($_POST['directory']) {
           $dir = 'some_directory'; // original directory name
           $command = 'mv "'.$dir.'" "'.$_POST['directory'].'";';
           // evaluates to: mv "original directory name" "new name";
           // the quotes are important for spaces.
           $result = system($command, $return);
           if ($return == 0) {
              // return code of zero is successful mv (on ubuntu linux, at least)
              echo 'Renamed directory successfully';
           }
           else {
              echo 'There was an error renaming directory.  Check your permissions?';
           }
        }
        

        That should do it.

        Edit: just a note - this does no checking of the input from the user, so they could potentially do some very serious damage to your filesystem. this is just example code, so clean it up and secure it as you need to.

          Ok, this does help a lot. How do I connect the form to create new directory to this script? Thank you very much for the awesome help.

            Oh, you want to make a whole new duplicate directory, not just rename it? Then try changing "mv" to "cp -r"

              Write a Reply...