Creating a folder (categories) within another existing folder (categories)

Hey everyone,

So I’ve got some some code for creating a directory (folder) but forgot to implement the option with creating another folder within a folder that was already created and then displaying that hierarchy on the same page. Here is what I have so far but I can post more code if it’s needed down the line, if I’ve forgotten something.

HTML:

<form id="form1" name="form1" method="post" action="" style="margin:0px;">
        
    <label for="category_title_box">New Category Title</label><br />
		  <input name="category_title_box" type="text" id="category_title_box" size="20" />
          <br />
	  <br />
		   Select a current Category:
		   <select id="Category_Select" name="Category_Select" height="27px">
			<option value="" selected="selected">-----</option>
				<?php 
					foreach(glob(dirname(__FILE__) . '/files/*') as $filename){
						$filename = basename($filename);
						echo "<option value='" . $filename . "'>".$filename."</option>";
					}
				?>
		   </select>
		   <br />

          <input name="button" type="submit" class="button_190" id="button" value="Add category" style="margin-top:10px;" />
</form>

The PHP along with the HTML:

// add new category 
if(isset($_POST['category_title_box']) and $_POST['category_title_box']!=''){ 
     
    $new_category_title = string_to_file_name($_POST['category_title_box']); 
     
    mkdir('files/'.$new_category_title); 
     
    header("Location: ".$gallery_url."/admin-categories?message=category added&message_type=success"); 
    exit; 
} 
// creates a 'category' folder within a 'category' folder 
elseif(file_exists($old_category_title) && $filename == true){ 
     
    $new_category_title = string_to_file_name($_POST['category_title_box']); 
     
    mkdir('files/'.$old_category_title.'/'.$new_category_title); 
     
    header("Location: ".$gallery_url."/admin-categories?message=category added&message_type=success"); 
    exit; 
}  

I’ve been looking at this for hours and my brain is sort of tired, so if I’ve missed something or completely messed it up, it’s probably because I don’t have fresh eyes.

Thank you for helping!

i don’t see what problem you have.

1 Like

The first bit of code where the comment “// add new category” is located creates a new folder (category). I tried to add to the my original code to have the capability to add “create” another folder (category) within an already existing folder (category) for projects and such. That bit of code starts with the comment, "// creates a ‘category’ folder within a ‘category’ folder ". For example.

Category 1 (folder)
- Title Project Name (folder)
Category 2 (folder)
- Title Project Name (folder)
…ect.

Anyway, the code above, just creates another folder, not a sub folder.

I’m a bit confused about the layout of the code. It seems that what you’re doing is checking to see if the user filled out the category_title_box field, and if they did, you create that folder in /files. If they did not fill out that form field, then you try to create the new category within $old_category_title, except I can’t see where that comes from. And that code will never work, because (a) it only executes if the user didn’t put anything in category_title_box, but (b) tries to use the value of category_title_box to create the new folder.

I would do something like:

  • Check to see if the user chose one of the existing categories in the Category_select option field.
  • If they did, create the new category inside that category
  • Otherwise, create it in the “root” folder.
1 Like

Would something like this work? I tried it and it just created a folder like all the others.

// add new category
if(isset($_POST['category_title_box']) and $_POST['category_title_box']!=''){
	
	$new_category_title = string_to_file_name($_POST['category_title_box']);
	
	mkdir('files/'.$new_category_title);
	
	header("Location: ".$gallery_url."/admin-categories?message=Category added&message_type=success");
	exit;
	
	// creates a 'category' folder within a 'category' folder
	elseif($_POST['Category_Select'] and $_POST['category_title_box'] != ''){
	
	$new_category_title = string_to_file_name($_POST['category_title_box']);
	
	mkdir('files/'.$old_category_title.'/'.$new_category_title, 0777);
	
	header("Location: ".$gallery_url."/admin-categories?message=category added&message_type=success");
	exit;
	}	
}

That won’t make any difference, because your first if() check will always give true - the user has put something in the new category title box. That if() doesn’t close before the elseif(), either, I’m surprised it doesn’t give a parse error. You need to check whether they’ve selected a folder to create the new one inside first, something like this pseudo-code:

Did user select something in 'Category_select'? 
  - Yes: Create folder inside the folder they chose
  - No: Create folder in main folder
1 Like

So forgive me for seeming dumb but I thought that:

// creates a 'category' folder within an existing 'category' folder
if(isset($_POST['Category_Select']) != ''){
	
	$new_category_title = string_to_file_name($_POST['category_title_box']);
	
	mkdir('files/'.$old_category_title.'/'.$new_category_title, 0777);
	
	header("Location: ".$gallery_url."/admin-categories?message=sub level category added&message_type=success");
	exit;
}

…would basically do what the pseudo-code suggests. Which is actually what I had in my head at accomplishing. So, I’m not sure why I wasn’t given an error with that if statement inside of the first one. Based on the pseudo-code above, are you saying that I should:

  • re-write the first if statement that basically creates a “main” directory folder after incorporating the if statement that checks to see if the user selected a dropdown list item…

  • to then create a sub directory within a main directory that was also created?

Are the exit functions at the end of each if statement, preventing the second if statement too be executed?

Here is what I’ve got so far, that still doesn’t work:

// add new category
if(isset($_POST['category_title_box']) and $_POST['category_title_box']!=''){
	
	$new_category_title = string_to_file_name($_POST['category_title_box']);
	
	mkdir('files/'.$new_category_title);
	
	header("Location: ".$gallery_url."/admin-categories?message=Category added&message_type=success");
	exit;	
}

$filename = $old_category_title;

// creates a 'category' folder within an existing 'category' folder
if(isset($_POST['Category_Select']) != ''){
	
	$new_category_title = string_to_file_name($_POST['category_title_box']);
	
	mkdir('files/'.$old_category_title.'/'.$new_category_title, 0777);
	
	header("Location: ".$gallery_url."/admin-categories?message=sub level category added&message_type=success");
	exit;
}

I can understand that there might be a frustration at this point with this topic, but I’m obviously not understanding some logic flow since I’m having to reach out for help. Please bare with me.

The logic issue is that the start of your code says this:

if(isset($_POST['category_title_box']) and $_POST['category_title_box']!=''){

So if I’m reading it correctly (and I might have become confused), you’re checking here to see whether the user put something in the name of the new category title text box. If they did, regardless of whether they selected a category in your <select> tag, then you create that category in the main “files/” folder, do a header redirect, then exit. Just after that if() block, you then check to see whether they selected a category to make the new category part of, and deal with that scenario.

The logic issue is that the user will always have put something in the new category title box (or at least you should be checking somewhere that they have before trying to use that value to create a folder), so the first if() is always true, so it always creates the new folder in the “files/” folder and then redirects, so it never gets a chance to see whether they really wanted it within a selected existing folder.

Hence the pseudo-code was: check first whether they chose a folder in the <select> tag, if they did then the new folder is a sub-folder within it, otherwise it’s in the main “files/” folder. Basically reversing the logic you have here - you have to check the <select> first because that decides where the new folder is going to be created.

I don’t know where your $old_category_title variable comes from, other than that the code seems OK except that you need to swap the code blocks around so that the one you have second is executed first.

ETA: All the above assumes I am interpreting the requirement correctly - you have a form with a text box and a select list of existing categories - the user types in a name of a folder, and optionally selects one of the values from the select list, if the user DOES NOT select anything from the list then the new folder is created in the “root” folder, otherwise it is created in the chosen folder.

1 Like

Ok, so I was understanding you correctly. Sorry for confusing you. I tend to over say things when it comes to explaining.

So after reading your comment, I realized that I’m not first collecting the GET variable of the existing directory name, which is where the $old_category_name variable should be coming from and stored into.

So with that said, Here’s what I have so far:

$old_category_title = trim(strip_tags($_GET['category_title']));

// category doesn't exist
if(!is_dir("files/".$old_category_title)){
	header("Location: ".$gallery_url."/admin-categories?message=That category does not exist&message_type=error");
	exit;
}

// creates a 'category' folder within an existing 'category' folder
if(isset($_POST['Category_Select']) and isset($_POST['category_title_box']) != ''){
	
	$new_category_title = string_to_file_name($_POST['category_title_box']);
	
	mkdir('files/'.$old_category_title.'/'.$new_category_title, 0777, true);
	
	header("Location: ".$gallery_url."/admin-categories?message=sub level category added&message_type=success");
	exit;
}

// add new category
elseif(isset($_POST['category_title_box']) and $_POST['category_title_box']!=''){
	
	$new_category_title = string_to_file_name($_POST['category_title_box']);
	
	mkdir('files/'.$new_category_title);
	
	header("Location: ".$gallery_url."/admin-categories?message=Category added&message_type=success");
	exit;	
}

How does that look?

Well, the first question would be “does it work”? One or two things:

$old_category_title = trim(strip_tags($_GET['category_title']));

I don’t see a field in your original HTML form with that name, isn’t it Category_Select? And in any case, you’re submitting the form data using ‘method=“post”’, so the $_GET array will be empty.

if(isset($_POST['Category_Select']) and isset($_POST['category_title_box']) != ''){

I don’t know whether you can use isset() there to see whether the user chose one of the selections - I can never remember whether it would send something through or not. So this might be perfectly fine, though as you’ve already created $old_category_title from the post variable, you could just use that.

One other thing - in your header redirects, you need to look at the value of the “message” field you’re trying to pass through, specifically you can’t include spaces without encoding them.

1 Like

well, the first if() statement is partially executing because I get the header redirect message of:

Location: ".$gallery_url."/admin-categories?message=Sub Level Category of ".$new_category_title." added&message_type=success"

However, the directory that I type in the input field to be placed as a sub directory from a category that I select from the select input, still displays as a main directory.

…also that $_GET is coming from the URL variable $gallery_url that is being passed in another php file which holds the value of dirname($_SERVER['SCRIPT_NAME']);. Also, at the bottom of the form, I’ve got another if() statement, which is using the count function, which is used for the $categories_array variable, which has array();, stored into it.

Within that if(), if got a foreach() loop which is basically looping through the existing categories.

…I realize that this is a little more complicated than what I’ve displayed here. Would you like me to possibly send a zip file of what I have so you can take a better look at it, for better understanding as to my issue?

…because that would be quite a bit of code to post here.

Are you sure? That is, does it have a value if you var_dump() it in your code? I hadn’t realised that when you submit the form to itself using method="post", it will retain the $_GET array that it had originally.

How does the value of $old_category_title relate to the selection the user makes in $_POST['Category_Select']?

I’m not sure sending me all the code would help much, it’d be better for you to extract the relevant stuff, and then anyone else that spots the stuff I miss can chime in.

1 Like

So it turns out that I’m just gonna scrap the code and rewrite everything from scratch to include the creating of sub-folders within a main folder. Sorry for any inconvenience. Thank you all for contributing!

Pity, it seems so close to working.

1 Like

Actually, I’m going to give it one more go at trying to achieve this issue. So I know my select variable is being stored correctly because I echoed out the variable on the page and it displayed correctly.

So now that I’m positive that there isn’t an issue with that part. I think that’s a step further in the right direction.

So with creating a main directory as an else if statement, and the first if statement creating a sub folder within an already created folder, would this look right? or have I gone back to what I originally had, logically?

// add new category
if(isset($_POST['category_title_box']) and $_POST['category_title_box']!=''){
	
	$new_category_title = string_to_file_name($_POST['category_title_box']);
	
	mkdir('files/'.$new_category_title);
	
	header("Location: ".$gallery_url."/admin-categories?message=Category added&message_type=success");
	exit;	
}elseif(isset($_POST['category_title_box']) and $_POST['category_title_box']!='' and $_POST['Category_Select']){
	
	$old_category_title = $_POST['category_title_box'];
	
	$old_category_title = $Category_Select;
	
	$new_category_title = string_to_file_name($_POST['category_title_box']);
	
	mkdir('files/'.$old_category_title.'/'.$new_category_title, 0777, true);
	
	header("Location: ".$gallery_url."/admin-categories?message=Sub Category added&message_type=success");
	exit;	
}

No, you’ve gone back to what you had before. The very first thing you have to check (after validating the new category input) is whether your user selected a category to create the new folder inside of. You’re not doing that, you’re doing it the wrong way around. Pseudo-code again:

Did they put something in the new category box? (only process if name not empty)
  Yes:
      Did they select a folder to create it within?
        Yes:
          Create it within that folder
        No:
          Create it within the main folder
1 Like

Yes I realize i had now. Sorry about that.

My question now is, with the $Category_Select variable holding the value of the selected item, would I need to create a new variable to pass into the mdir(); function or do I simply pass that variable as is.

Also, don’t I need to check if that variable which holds the selected category, exists as an existing folder? if that’s the case would I do something like:

if(is_dir($Category_Select)){
    create folder within parent directory that is also contained in variable
}

Isn’t the $Category_Select variable chosen from a list that you populated by reading through the directory? If it is, then in theory it must exist otherwise it wouldn’t be in the list. But it’s good practice to check that it exists, in case it’s been deleted in the time between drawing the select list and the user filling out the form. But an error check around mdir() would deal with that, and cause the select list to redraw.

If the value coming off the select list is the actual directory name, you could just use the $_POST variable once you’ve validated it to make sure someone hasn’t messed with it.

1 Like

Nevermind. What I’ve got here works:

// does category box have value
if(isset($_POST['category_title_box']) and $_POST['category_title_box']!=''){

	// was Category_Select selected
	if(isset($_POST['Category_Select'])){
		//yes, create folder within that folder
		$new_category_title = string_to_file_name($_POST['category_title_box']);
		
		mkdir('files/'.$Category_Select."/".$new_category_title);
		header("Location: ".$gallery_url."/admin-categories?message=Sub Category added&message_type=success");
		exit();
		
	}
	// add new category
	elseif(isset($_POST['category_title_box']) and $_POST['category_title_box']!=''){
	
	$new_category_title = string_to_file_name($_POST['category_title_box']);
	
	mkdir('files/'.$new_category_title);
	
	header("Location: ".$gallery_url."/admin-categories?message=Category added&message_type=success");
	exit;	
	}
}

Thank you for enlightening me with the psudo-code to help me figure this out! I may create another topic regarding something similar regarding this whole application but I’m hoping I don’t have to and can go from here. Thanks again!

1 Like

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