Creating a directory

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
admaster
Forum Newbie
Posts: 12
Joined: Tue Nov 28, 2006 7:16 pm

Creating a directory

Post by admaster »

Sorry about this, but I'm a newbie at php.

I'm trying to create a type of program that will allow me to input a name in the first page, adn create a directory iwith that name.

This is how far I got.

input.php:

Code: Select all

<html>
<body>
<title>Create User Form (Beta)</title>
</script>
<form name="form1" method="post" action="create.php">
<p>Enter the username you want to create</p>
<p>
<input name="$cuser" type="text" id="$cuser" maxlength="100">
</p>
<p>
<input type="submit" name="Submit" value="Submit">
</p>
</form>
</body>
</html>
create.php:

Code: Select all

<?
mkdir($cuser, 0777);
?>
Thanks a lot. :)

Jcart | Changed thread title appropriately
Last edited by admaster on Tue Nov 28, 2006 7:25 pm, edited 1 time in total.
User avatar
evilchris2003
Forum Contributor
Posts: 106
Joined: Sun Nov 12, 2006 6:43 am
Location: Derby, UK

Post by evilchris2003 »

do you mean a file directory
or database table to create login (a guess from your HTML code)
admaster
Forum Newbie
Posts: 12
Joined: Tue Nov 28, 2006 7:16 pm

Post by admaster »

I don't want to have a sql database. I want a small page which asks you to input a name of a directory. Then, when pressing submit, the directory is completed. It doesn't work for me.
User avatar
evilchris2003
Forum Contributor
Posts: 106
Joined: Sun Nov 12, 2006 6:43 am
Location: Derby, UK

Post by evilchris2003 »

im sorry i really dont understand what you are trying to do
admaster
Forum Newbie
Posts: 12
Joined: Tue Nov 28, 2006 7:16 pm

Post by admaster »

Okay. I want a screen that has a box asking for some value. Then, afte rclicking a button, it parses a script, and creates a directory with teh name of what was inputted on the first page. Is it clear now?
User avatar
evilchris2003
Forum Contributor
Posts: 106
Joined: Sun Nov 12, 2006 6:43 am
Location: Derby, UK

Post by evilchris2003 »

but what sort of directory there are several ways to interpret this

for example what do you want the directory to do once it is created
admaster
Forum Newbie
Posts: 12
Joined: Tue Nov 28, 2006 7:16 pm

Post by admaster »

Nothing. Just create one.

I wnat a file-folder type thingy
User avatar
Zoxive
Forum Regular
Posts: 974
Joined: Fri Apr 01, 2005 4:37 pm
Location: Bay City, Michigan

Post by Zoxive »

you need a full or absolute path name for mkdir();

example..

Code: Select all

mkdir("/www/htdocs/public_html/home/newfolder/", 0700);
and you can do something like...

Code: Select all

if(mkdir("/www/htdocs/public_html/home/newfolder/", 0700)){
     echo 'Success!';
}else{
    echo 'Making Directory Failed';
}
Last edited by Zoxive on Tue Nov 28, 2006 7:52 pm, edited 1 time in total.
User avatar
evilchris2003
Forum Contributor
Posts: 106
Joined: Sun Nov 12, 2006 6:43 am
Location: Derby, UK

Post by evilchris2003 »

well im fairly new to PHP and i havent come across it i have googled about a bit to maybe im using the wrong keywords but sorry i cant help
User avatar
shoebappa
Forum Contributor
Posts: 158
Joined: Mon Jul 11, 2005 9:14 pm
Location: Norfolk, VA

Post by shoebappa »

Hopefully register globals is not on for security reasons. You should use:

Code: Select all

<? 
mkdir($_POST["cuser"], 0777); 
?>
If the form action was the GET method, you would use the $_GET... Also if you aren't *nix you could prolly use just mkdir($_POST["cuser"]);

also don't use $ in the HTML part!

Code: Select all

<html> 
<body> 
<title>Create User Form (Beta)</title> 
<form name="form1" method="post" action="create.php"> 
<p>Enter the username you want to create</p> 
<p> 
<input name="cuser" type="text" id="cuser" maxlength="100"> 
</p> 
<p> 
<input type="submit" name="Submit" value="Submit"> 
</p> 
</form> 
</body> 
</html>
Last edited by shoebappa on Tue Nov 28, 2006 8:00 pm, edited 1 time in total.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Hold on a moment there :?

Firstly let me tell you that we are all volunteers here, and will help those at our own wishes. With that in mind, do you think it's fair to tell us how quickly and important this is that you get it fixed? If it is important then you should considering hiring a professional.

I've already deleted a post, and changed your thread title.. so lets not let this happen again ok? :wink:


Now as for your problem, it looks like your server configuration has register globals set to off (which is a good thing). That means that you have to reference global variables (such as form variables) why their global namespace.

Code: Select all

mkdir($_POST['cuser'], 0777);
I also noticed in your html you are trying to reference a php variable. I don't think you meant to do that so lets go ahead and remove it.

Code: Select all

<input name="$cuser" type="text" id="$cuser" maxlength="100"> 
becomes

Code: Select all

<input name="cuser" type="text" id="cuser" maxlength="100"> 
There are some security implications with this.. you should

a) check whether the directory exists or not prior to creating it, file_exists() may be of interest
b) You should consider creating them the directories in a subdirectory

Code: Select all

mkdir('users/'. $_POST['cuser'], 0777);
c) and lastly you should check whether they are entering legitimate data into the form

empty(), isset(), ctype_alpha(), preg_match() all may be if interest
User avatar
shoebappa
Forum Contributor
Posts: 158
Joined: Mon Jul 11, 2005 9:14 pm
Location: Norfolk, VA

Post by shoebappa »

Zoxive wrote:you need a full or absolute path name for mkdir();
I think if you don't it makes it in the same directory as the current script...

Edit: Confirmed on PHP 5.1
Last edited by shoebappa on Tue Nov 28, 2006 8:04 pm, edited 1 time in total.
User avatar
Zoxive
Forum Regular
Posts: 974
Joined: Fri Apr 01, 2005 4:37 pm
Location: Bay City, Michigan

Post by Zoxive »

shoebappa wrote:
Zoxive wrote:you need a full or absolute path name for mkdir();
I think if you don't it makes it in the same directory as the current script...
Yup, you never know where he could be looking, it could acually create it and he could of easily been looking in the wrong folder in the first place. But as it has already been pointed out, he had a '$' which caused the whole mess.
admaster
Forum Newbie
Posts: 12
Joined: Tue Nov 28, 2006 7:16 pm

Post by admaster »

Jcart wrote:Hold on a moment there :?

Firstly let me tell you that we are all volunteers here, and will help those at our own wishes. With that in mind, do you think it's fair to tell us how quickly and important this is that you get it fixed? If it is important then you should considering hiring a professional.

I've already deleted a post, and changed your thread title.. so lets not let this happen again ok? :wink:
I'm very sorry. I'm a newbie here. Sorry. :oops:
Post Reply