Copy files to new directory

I have made a directory from user input on a form but I am having problems copying to the new directory I made. How do I reference the new directory in the copy function. i.e. c:/inetpub/wwwroot/$new_dir ? Should I make a variable that holds the new directory and just copy to it or not? Thanks for any help.

<?php if (isset($_POST['submit'])) { $name = $_POST['comp_name']; mkdir ("c:/inetpub/wwwroot/$new_dir); copy ('c:/intepub/wwwroot/some_folder/file.txt' , 'c:/inetpub/wwwroot/$new_dir/file.txt') ?>

Hi brentmilne,

You are setting $name to a $_POST variable, but not using it anywhere. Additionally, you are attempting to use a variable, “$new_dir” that isn’t defined.

In the code below, I added an assignment: $new_dir = $name;. I didn’t know what value you wanted, so I guessed that it should be the passed $_POST variable. If this is correct, you should delete this line and simply change the $name variable with $new_dir.

See if this works for you (I am not on my testing server to check it)…[php]<?php
if (isset($_POST[‘submit’])) {
$name = $_POST[‘comp_name’];

$new_dir = $name; // I have no idea if this is really what you want…

  mkdir ("c:/inetpub/wwwroot/$new_dir");
  copy ("c:/intepub/wwwroot/some_folder/file.txt" , "c:/inetpub/wwwroot/$new_dir/file.txt");

}
?>[/php]

Your mkdir was missing a closing double quote. You were also missing a semi-colon at the end of your copy line and a closing bracket for the if statement.

I also changed the copy content from single quotes to double quotes. PHP will not attempt to substitute values for variables that are contained in single quotes- it will consider them to be literal text. There are other ways of doing this, but double quotes should work fine in this situation.

Let me know…

Sponsor our Newsletter | Privacy Policy | Terms of Service