Jump to content

Recommended Posts

Hi all! I'm building a document management system and I've hit a snag. I'm relatively new, so this might be obvious to someone else.

 

A user can add folders into the system (kinda like Windows Explorer) and then add folders IN those folders. I have a database set up to track the child/parent relationships but I'm having trouble with the actual creation of the directory on the server.

 

I pass the "add folder" script the NAME of the new folder as well as the PATH of the new folder. If I'm in the root directory and I add a "TEST" folder I pass "TEST" as the folder name and "" as the path, because we're adding at the root.

 

If I've added "TEST" and I want to add "IN TEST" inside the "TEST" folder I send "IN TEST" as the new folder name and "TEST" as the path. My code then adds a "/" and calls mkdir. So the path passed to mkdir is "IN TEST/TEST".

 

Here's the code:

                 $dir_path		= $_POST['dir_path'];	

	/**************************************************
	 figure out where on the server this will go
	 ie: admin/myfolder and create the full path
	 for using the mkdir function
	 **************************************************/
	if ($parent_id > 0){
		$path = $dir_path."/".$_POST['cat_name'];
	} else $path = $_POST['cat_name'];

	/****************************************
	  add the directory on the server
	 ****************************************/
	mkdir($path);
	chmod($path,0777);

 

This ONLY works at one level. If I want to add a THIRD level inside "IN TEST" I get this error:

 

"Warning: mkdir(second in admin/test): No such file or directory in ...".

In this example the path sent to the script was "admin/second in admin" and then my script SHOULD have added "/test" to the path and called mkdir to create a directory admin/second in admin/test.

 

Should I be doing this differently? Any advice is greatly appreciated. Oh, and any advice on how to deal with folder permissions would be great as well. 777 seems like a bad idea, but i need people to be able to add things to the server!

 

Thanks

Mike

Can you post what $_POST['dir_path'] looks like and how you come to it?

 

If you would be willing, please post the code for the page that Post to this script and the rest of this script.  Please put it in code boxes and include the <?php ?> so that it gets colorized.

 

~D

okay i had a ton of questions.. but instead.. i thought i'll create an example and you could add it to your code.. hope this helps (added comments to help)

<?php
//Where are we starting from
$StartDir = dirname(__FILE__)."/FileStuff/";
//What folder are we in (follow from above)
$SubPath = (isset($_GET['dir']))?$_GET['dir']:"";

//create folder
if(isset($_GET['mk']))
{
	//Check it doesn't already exist
	mkdir($StartDir.$SubPath."/".$_GET['mk'],0777);
}

//List files
if ($handle = opendir($StartDir.$SubPath."/")) 
{
	while (false !== ($file = readdir($handle))) 
	{
		//don't list . or ..
		if ($file != '.' && $file != '..') 
		{
			///File or folder ??
			if(is_dir($StartDir.$SubPath."/".$file)) 
			{
				//Folders
				echo "<a href=\"?dir=$SubPath/$file\">$file</a><br>";
			}else{
				//Files
				echo "$file<br>";
			}
		}
	}
	//create folder (only creates one named "test")
	//but you can add a form etc..
	echo "<a href=\"?dir=$SubPath&mk=TEST\">Create Test folder</a><br>";
}
?>

A few thoughts after looking at the code .....

 

to MadTechie: You're obviously REALLY good at this stuff, so thanks for your time. I'm not sure exactly how your example works. So I'm going to try to figure it out and perhaps you can correct me:

- assign $startdir as the name of the dir where the php code is being executed and adding "/filestuff/" as the first level of directories

- $subpath is the path passed in (in my case it would be "$dir_path')

- $mk is the name of the new directory (in my case it would be "cat_name")

- your mkdir call doesn't look much different from mine, other than I do all the concatenation to "$path" ($path = $dir_path."/".$_POST['cat_name']) and I do a chmod afterwards. What IS the difference?

- I feel a bit dense, but I don't fully understand the 'list files' section. You're just going through the NEW directory displaying files and folders, yes?

 

Anyway, I appreciate your help!

 

 

To Daleeburg: Below is the POST code. I use "cat_" to refer to categories, which is what I call folders in my database. So "cat_name" is just the name you want to give the folder.

$dir_path is a variable that I pass along when "digging" into nested folders. So if I'm in 'root' the $dir_path is ''. If I'm in root/test then the $dir_path is '/test' and it is diplayed along the top of the page just like "PHP Freaks Forums > PHP and MySQL > PHP Help".

 

The new "cat_name" entered by the user will be concatenated onto the $dir_path.

 

 

 

<?php

$display_block .= "<form method=\"post\" action=\"".$_SERVER["PHP_SELF"]."\">

		<p>Folder Name<br>
		<input type=\"text\" name=\"cat_name\" size=\"20\" maxlength=\"75\">
		</p>

		<p>Description<br>
		<input type=\"text\" name=\"cat_descr\" size=\"20\" maxlength=\"75\"></p>";

// pass parent id to POST block
// parent_id is used in the database to indicate that the NEW folder will be nested in a PARENT folder
$display_block .= "<input type=\"hidden\" name=\"parent_id\" value=\"".$parent_id."\">";	

// pass path to POST block
$display_block .= "<input type=\"hidden\" name=\"parent_name\" value=\"".$dir_path."\">";		

$display_block .= "<p><input type=\"submit\" name=\"submit\" value=\"Add Folder\"></p></form>






?php>

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.