Newgrounds.com — Everything, By Everyone.

Checking login status…

USERNAME:

PASSWORD:

Logging in…

Logged in as:
.
Logging out…
Inbox My Account Log Out


Forum Topic: PHP: Creating a new directory

(253 views • 3 replies)

This topic is 1 page long.

<< < > >>
Thinking

Momo-the-Monkey

Reply To Post Reply & Quote

Posted at: 7/22/07 11:08 PM

Momo-the-Monkey EVIL LEVEL 29

Sign-Up: 10/15/05

Posts: 3,097

PHP: Main

What will we be talking about:

In this tutorial, I'm going to show you how to use the php functions mkdir and rmdir

Who should be using this tutorial:

If you're new to php, check out the link above. You need to have a solid foundation of PHP before reading this tutorial. Otherwise, you'll be confused out of your mind O.O

Brief Explanation

mkdir and rmdir aren't REAL common, but it's one of the only functions in PHP so far that can create/remove a directory

Ok, let's begin!!!!

=================
Using mkdir
=================

Mkdir has 4 parameters and only one is required.
The parameters for mkdir are as follows:

Path - The directory (the complete directory, if you don't know it, just use $_SERVER['DOCUMENT_ROOT']) that is needed to be made

Mode - The number at which the directory will be chmoded at.

Recursive - Not exactly sure on what this does. It's a true or false value (user specifies). I'm guessing it's whether or not to create all directories that's your pointing to, if they don't exist

Context - Read more here

----------------------

To use mkdir, you will probably not need to use recursive or context. But mode comes in handy.
Here's a simple set up of mkdir

mkdir('/path/to/complete/dir');

This will set up the folder 'dir' in 'path/to/complete/';
Notice there is no ending forward slash. One is not needed, because it outputs errors.
Here's what you'll most likely be using:

$path = $_SERVER['DOCUMENT_ROOT'] . '/newdir';
mkdir($path);

This, as it seems, will create the directory "newdir" in the uppermost level of your site (/home/my_user/public_html/)
To chmod the directory is a little bit different than just:

mkdir($path, 0777);

Because the above example will leave you with a directory chmoded to 0755. Why you say?
Quoted from PHP.NET

"
The default value of umask, at least on my setup, is 18. Which is 22 octal, or
0022. This means that when you use mkdir() to CHMOD the created folder to 0777,
PHP takes 0777 and subtracts the current value of umask, in our case 0022, so
the result is 0755 - which is not what you wanted, probably."

Yup, this means we need to bring in another function, called umask to get us where we want to be.

The fix for this would be to simply have this line of code before your mkdir:

$umask = umask(0);
mkdir($path, 0777);

Now it works fine! To bring it back to normal, just add $undo_umask = umask($umask)

Note: the variable names don't matter

Moving on then! If you need to create a subfolder within a non-existent folder, recursive is the handy tool to help you out! It's the parameter after mode (in order: path, mode, recursive, context) and can be used like so:

The following lines of code will create the directory "newdir" with a sub-directory "subdir" chmoded to 0777:

$umask = umask(0);
mkdir('/newdir/subdir', 0777, TRUE);

NOTE: After extensive testing, it seems that if you don't use the code above, or fail to include $umask = umask(0);, you new directory will be set at 0755 and won't be able to be removed.
However, without recursive on, it delete's fine.

==================
Using rmdir
==================

Whoopee! Now we have a directory, but it's old, and there's millions, so we need php to delete it!!
What ever shall we do? *Dun, dundun, daaaa!!!, it's rmdir() to the rescue!!'

Ok, so it's not that big of a deal, but when you're dealing with a load of folders, in conjuction with either arrays or foreach or while loops, this sure does come in handy :p

rmdir only has 2 parameters:

Path - The already existing path to a directory on your server or localhost

Context - See previous context in mkdir for link to read more

--------------------

rmdir only requires one parameter. The path.
But, the path to the directory you want to remove, must be the same as when you created it.
So for example, this script will not work, if the script is located at /home/my_user/public_html/remove.php

rmdir('/file');

This is also the same with mkdir

To solve the problem easily, just add a $_SERVER['DOCUMENT_ROOT'] in front of the file!
To remove the newly created /newdir/subdir, you might think to use the code below:


$path = $_SERVER['DOCUMENT_ROOT] . '/newdir/subdir';
rmdir($path);

But it doesn't work, because we have a sub-directory inside it! rmdir won't directly remove everything inside a folder.

As like before, the ending forward slash will just produce an error, so don't use it!

I'll be nice and show a work-around for this one :p
There is a nice function a guy made. You can find it here

But for deleting just one folder with absolutely nothing inside it, rmdir works fine. Otherwise, use the given function provided in the link.

======================

Well, that's all I have for this tutorial. As I said before, (or tried to) with mkdir, and rmdir (with the function provided), it doesn't look like you can do much.

But with something like the code below, it can prove to be useful, especially with file uploads ;)

for($i=0;$i<=10;$i++){
$umask = umask(0);
$path = $_SERVER['DOCUMENT_ROOT'];
$path .= '/'.$i;
mkdir($path, 0777);
}

This (by the way) creates directories 0 through 9 in your top level directory not past public_html :)

Alright, well, I guess that just about raps it up. I'm going to leave you off to explore the vast nature of mkdir, rmdir, and the function given in the link. If you use the function given in the link, please give proper credit, and do not edit his comments.

If you have any questions, comments, concerns, regards, or any other hazards to health, just post it here, or PM me Momo-the-Monkey

<3 Momo
<.V

You should see Gir's Soundboard...
{ PHP: Main | | Java: Main }
Every Villian Is Lemons

BBS Signature

None

elbekko

Reply To Post Reply & Quote

Posted at: 7/23/07 05:29 AM

elbekko EVIL LEVEL 16

Sign-Up: 07/23/04

Posts: 6,302

You forgot a minor detail. If the parent folder isn't properly chmodded, none of this will work ;)
Otherwise, not bad.

"My software never has bugs. It just develops random features. " - Unknown

[ PHP: Main | Omigod, a blog ]

BBS Signature

Elated

Storm

Reply To Post Reply & Quote

Posted at: 7/23/07 07:05 AM

Storm EVIL LEVEL 05

Sign-Up: 05/19/06

Posts: 1,107

Nice tutorial, this is quite good when used with flat files and stuff.


Goofy

Momo-the-Monkey

Reply To Post Reply & Quote

Posted at: 7/23/07 11:22 AM

Momo-the-Monkey EVIL LEVEL 29

Sign-Up: 10/15/05

Posts: 3,097

At 7/23/07 05:29 AM, elbekko wrote: You forgot a minor detail. If the parent folder isn't properly chmodded, none of this will work ;)
Otherwise, not bad.

I did run into that problem myself, but I didn't think it was a global problem. I figured since the FTP worked, public_html or whatnot should be accessable

But you're right, You need to make sure the folder you're trying to create this in is chmoded to 0777 :)

You should see Gir's Soundboard...
{ PHP: Main | | Java: Main }
Every Villian Is Lemons

BBS Signature

All times are Eastern Daylight Time (GMT -4) | Current Time: 11:34 PM

<< Back

This topic is 1 page long.

<< < > >>
You need a Grounds Gold Account to post on the NG BBS! If you don't have one, click here to sign up now! It's fast, free, and easy — and opens up tons of great NG features!