Directories

My post title may be a little misleading but what I want to be able to do two things with this.

One create a new directory, and within the directory that has been created, create 3 more folders.

So lets say I have a register form on my website right, I want it so that when the user clicks the submit button (register), it creates a folder with their name/username, and within that folder that has been created with their name/username 3 more folders are created inside, i.e. folder1, folder2, folder3.

So now when I look on my server it would say my username: admin33 and within admin33 I have folder1, folder2, folder3.

What I want to know is, is there someway to do that?

I have this code that makes the directory, but then my brain shuts down when I want to modify it and make those other 3 foldersā€¦

<?php
$foldername = $_POST["foldername"];
   mkdir($foldername, 0700);
?>
<form action="testersduasd.php" method="post">
      <input  name="foldername" id="foldername" >
      <input type="submit" value="upload" />
</form>

Did you try

$foldername = $_POST["foldername"];

mkdir($foldername, 0700);

for ($i = 1; i <= 3; i++) {
    mkdir($foldername . '/folder' . i, 0700);
}

If an error is produced, try preceding the incremental i value with the $ sign.

This works fantastically well, but how would I make it so that the folders have different names
so for example: youtube, google,and sitepoint, would be the folder names instead of folder1, folder2, folder3

Oops! My bad. That really was just a typo. :blush:

I would put the foldernames in an array, and use $i as the index:

$subfolder = array('google', 'sitepoint', 'youtube');
for ($i = 0; $i < 3; $i++) {
    mkdir($foldername . '/' . $folder[$i], 0700);
}
1 Like

Never mind, I got it. Please tell me if itā€™s possible to simplify this though.

<?php
$foldername = $_POST["foldername"];

mkdir($foldername, 0700);

mkdir($foldername . '/google' , 0700);
mkdir($foldername . '/sitepoint' , 0700);
mkdir($foldername . '/youtube' , 0700);
?>
<form action="make_directory.php" method="post">
      <input  name="foldername" id="foldername" >
      <input type="submit" value="upload" />
</form>

Thank you very much WebMachine

Somehow my answer to that question ended up above your question. Lol

I thought I was losing it when your code didnā€™t work but I realized instead of it being $folder[$i] it should be $subfolder[$i] so it comes out

$foldername = $_POST["foldername"];

mkdir($foldername, 0700);

$subfolder = array('music', 'video', 'pictures');
for ($i = 0; $i < 3; $i++) {
    mkdir($foldername . '/' . $subfolder[$i], 0700);
}

Thank you for all your help man :slight_smile:

To eliminate possible errors which may occur when using mkdir(ā€¦);, try using is_dir(ā€¦) to ensure the proposed new directory does not already exist.

donā€™t use statements like that - either just reference $_POST[foldername'] everywhere and donā€™t create $foldername as an alias for it at all or VALIDATE the content of $_POST[foldername'] so that you know that $foldername contains a valid folder name and not anything at all.

Sorry. I really should have stayed away from my computer tonight. Thanks for picking up on my error.

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.