Jump to content
xisto Community
Sign in to follow this  
vujsa

Automated File Structure Creation Script As Requested By Mark420

Recommended Posts

While chatting with Mark420 today on the shoutbox, he mentioned that he was looking for a script to create his entire folder structure with just a click of a button. For example, he wanted the following folders created in his root folder by just clicking submit.

/images

/images/thumbs

/images/icons

/css

/javascripts

/content

/content/articles

/content/tutorials

Presumably this would be used for some type of installation system or other quick server setup situation.

 

Anyhow here is what I threw together for him:

<?php /* ************************************************************ *//*                                                              *//*                     Configuration area                       *//*                                                              *//* ************************************************************ */// The total number of input boxes to show for the addition of foldernames:$size = 14;// Default root path$root = "/home/username/public_html";// An array of the default folders to be automatically inserted into the form.$default_folders = array('/images','/images/thumbs','/javascripts','/private','/css','/content');// File Mode (Permissions) - Ignored by Windows$mode = "0755";/* ************* DO NOT EDIT BELOW THIS LINE! ************* *//* ************* DO NOT EDIT BELOW THIS LINE! ************* *//* ************* DO NOT EDIT BELOW THIS LINE! ************* */function build_input_field($type, $name, $value = '', $size = '', $maxlength = '', $option = '', $prepend = '', $append = '') {// Function written by Marcus L. Griswold (vujsa)// Can be found at [url="http://forums.xisto.com/no_longer_exists/ Do not remove this header!    $field = "<input type=\"$type\" name=\"$name\"";    if($value) $field .=  " value=\"$value\"";    if($size) $field .=  " size=\"$size\"";    if($maxlength) $field .=  " maxlength=\"$maxlength\"";    if($option) $field .=  " $option";    $field .= ">";    $field = $prepend.$field.$append;    return $field;}if(!$_POST){	// Do Input form	$page_title = "Handy PHP Folder Creation System - Start\n";	$body = "\t\t<div style='text-align: center; font-size: 18pt; font-weight: bold; color: navy;'><span>Handy PHP Folder Creation System</span></div><br />\n";		$body .= "\t\t<br />\n";	$body .= "\t\t\t<form action = '' method = 'post'>\n";	$input_field .= build_input_field('input', 'root', $root, 50, 255, 'title = "Insert the exact root path to the new folder you want to create."', "\t\t\t\tRoot Path: ", "<br />\n\t\t\t\t<br />\n");	for($x=1; $x<=$size; $x++){		$name = 'folder' . $x;		$y = $x - 1;		if($default_folders[$y]){			$value = $default_folders[$y];		}		else{			$value = NULL;		}		if($x < 10){			$z = "   ";		}		else{			$z = " ";		}					$input_field .= build_input_field('input', $name, $value, 30, 255, 'title = "Insert the relative path of your new folder without a trailing slash."', "\t\t\t\tFolder $x:$z", "<br />\n");	}$input_field .= build_input_field('submit', 'submit', 'submit', '', '', '', "\t\t\t\t<br />\n\t\t\t\t    ", "    ");$input_field .= build_input_field('reset', 'reset', 'reset', '', '', '', "    ", "<br />\n");$body .= $input_field;	$body .= "\t\t\t</form>\n";}else{	// Do the folder creation	$page_title = "Handy PHP Folder Creation System - Finished\n";	$body = "\t\t<div style='text-align: center; font-size: 18pt; font-weight: bold; color: navy;'><span>Handy PHP Folder Creation System</span></div><br />\n";		$body .= "\t\t<br />\n";	$root = $_POST['root'];	for($x=1; $x<=$size; $x++){		$folder_name[$x] = $_POST['folder' . $x];		$full_path = $root . $folder_name[$x];		if($folder_name[$x] && !file_exists($full_path)){			if(@mkdir($full_path, $mode)){				$body .= "<b>" . $full_path . "</b><span style='color: #230BFD;'> Was Created!</span><br />\n";			}			else{				$body .= "<b>" . $full_path . "</b><span style='color: #FF1515;'> Was not created: Unknown Error!</span><br />\n";			}		}		else if($folder_name[$x] && file_exists($full_path)){			$body .= "<b>" . $full_path . "</b><span style='color: #FF1515;'> Was not created: Directory Already Exists!</span><br />\n";		}	}}?><html>	<head>		<title>			<?php echo $page_title; ?>		<\title>		<meta name="description" content="">		<meta name="keywords" content="">		<meta name="author" content="M. L. Griswold / HandyPHP.com">	</head>	<body bgcolor="#ffffff" text="#000000" link="#0000ff" vlink="#800080" style="font-family:verdana; font-size: 10pt;"><?php echo $body; ?>			<br />			For technical support for this script, please visit <a href="http://forums.xisto.com/no_longer_exists/; title="Handy PHP - A PHP Resource Site">Handy PHP</a>.  			<br />			<hr />			<br />			<center style="font-size:8pt;">Š2007 <a href="http://forums.xisto.com/no_longer_exists/; title="Handy PHP - A PHP Resource Site">Handy PHP</a></center>	</body></html>

There a couple of notes about this script though:

This script offers absolutely no security features. This means that if made publicly available, anyone on the internet could create new directories on your server. You need to protect yourself by placing this script in a private or password protected directory on your server.

 

If used on a unix/linux machine, the new folders will be owned by the server and not the account owner because the directory was actually created by the server!

 

You must create the parent before the child! if you want /images/icons, you must either create /images first or is must already exist on the server!

 

By modifying the configuration section of the script, you can make the scripts default values match your environment which will make the script easier to use.

 

There is one thing that I request:

Please leave the entire build_input_field function section of the script intact. It offers great flexability if use correctly and shouldn't require any modification for inclusion in any script. It is a function that I personally wrote and made publicly available to everyone to use. For more information about this and other functions I have written, please visit Handy PHP. Basically, if you want to use this function or redistribute it, leave it intact with the credit header. However, if this function inspires you to rewrite or recreate a function that performs the same task, I welcome you to do so.

 

I'll keep you posted as to the developement of an FTP version of this script via this topic.

 

vujsa

Share this post


Link to post
Share on other sites

I should probably start contributing to HandyPHP though, but how to put it, it's not the most pleasant looking site, it needs work.Your function build_input_field could be improved.Firstly, you should use HTML's defaults for the input element, this is just like doing <input /> what would you expect it to be if displayed in your browser?You should then make sure you know all the attributes for the types of input and what attributes it can accept (this is where you create validations for the type of data it can accept), that way you can make sure you limit the attributes of the input to only using what it can based on the type specified, must ensure no exploitation too.If I said the type is password, I can't use the attribute checked="checked" because it's the wrong type. Also I see that you force some attributes, that aren't really required, nor do you properly check their value existence, you would have to turn on error_reporting(E_ALL); to see what I mean.I'm sure there's more that can be done, maybe more along the lines as to creating a complete HTML DOM API or specific areas like forms, tables, lists, etcNow for the whole script, to me it's not flexible enough to be used widely, you need to give the user more control, allow them to make changes without altering the file itself, you will need to put in security for it too.I notice some things in your notes above too, these things you could solve through your script, those would be beneficial to have.I might take the opportunity to work on this script when I find time to.Cheers,MC

Share this post


Link to post
Share on other sites

Hey Vujsa...I finally got time over the weekend to mess around with the script..been too busy this week with work and with kids;((( But its great..I changed a few of the folder names but apart from that it worked fine..cheers!!!!!Marky;)

Share this post


Link to post
Share on other sites

Mark420, glad to hear that the script is working exactly the way it was indended.I developed an FTP version that does the same process but offers the security benefits of requiring the FTP login information each submission. Also, the FTP version will ensure that the directories you create have the proper ownership.Let me know if you are interested in it and I'll pass it along.vujsa

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | 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.