Current location: Hot Scripts Forums » Programming Languages » PHP » [SOLVED] Check if a directory exists, If not create it


[SOLVED] Check if a directory exists, If not create it

Reply
  #1 (permalink)  
Old 02-04-06, 05:38 AM
xxvatarxx xxvatarxx is offline
Wannabe Coder
 
Join Date: May 2005
Posts: 212
Thanks: 0
Thanked 0 Times in 0 Posts
[SOLVED] Check if a directory exists, If not create it

How do i check if a directory exists and if it does then i go onto part c, then if it doesn't go to part b

E.g

if "/$username" exists //Part A

then

create /$username //Part B

else //Part C

Don't know if the statements are correct, fairly new to all this

My first attempt at writing something, i have the make directory code already, So that was only a basic outline
Reply With Quote
  #2 (permalink)  
Old 02-04-06, 05:42 AM
Nico's Avatar
Nico Nico is offline
Community Leader
 
Join Date: Sep 2005
Location: Spain
Posts: 8,075
Thanks: 11
Thanked 88 Times in 83 Posts
Reply With Quote
  #3 (permalink)  
Old 02-04-06, 06:20 AM
xxvatarxx xxvatarxx is offline
Wannabe Coder
 
Join Date: May 2005
Posts: 212
Thanks: 0
Thanked 0 Times in 0 Posts
thanks, i looked on that site but didn't find it (must be my short attention span )
Reply With Quote
  #4 (permalink)  
Old 02-04-06, 07:33 AM
FiRe FiRe is offline
Code Guru
 
Join Date: Oct 2004
Location: UK
Posts: 801
Thanks: 0
Thanked 0 Times in 0 Posts
surely its easier to use is_dir() ? http://uk.php.net/manual/en/function.is-dir.php
__________________
Alexa Share <-- Trade virtual shares in websites with this online game.

codR.us <-- Submit and vote for your favorite code snippets with codR.us.

XEWeb.net <-- The ultimate PHP resource network.
Reply With Quote
  #5 (permalink)  
Old 02-04-06, 07:59 AM
Barnz1986 Barnz1986 is offline
Aspiring Coder
 
Join Date: Jan 2006
Posts: 506
Thanks: 0
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by xxvatarxx
How do i check if a directory exists and if it does then i go onto part c, then if it doesn't go to part b

E.g

if "/$username" exists //Part A

then

create /$username //Part B

else //Part C

Don't know if the statements are correct, fairly new to all this

My first attempt at writing something, i have the make directory code already, So that was only a basic outline
Like this i think: -

Code:
If(file_exists($username)) //part A
{
mkdir($username); //part B
}
else
{
//part C
}
I think the file_exists() function can be used to check if directories exists aswell as files.

mkdir() is the function to create new directories.
Reply With Quote
  #6 (permalink)  
Old 04-03-08, 01:53 PM
scottjcampbell scottjcampbell is offline
New Member
 
Join Date: Apr 2008
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
?

Sorry, this is ages ago, but my code:

PHP Code:

<?

include("include/session.php");
if (
file_exists($session->username))
{
mkdir($session->username);
echo 
"made";
}
else
{
echo 
"already made";
}
?>
always comes up as "already made" even if its not, what am i doing wrong?
Reply With Quote
  #7 (permalink)  
Old 04-03-08, 02:31 PM
Jay6390's Avatar
Jay6390 Jay6390 is offline
Code Master
 
Join Date: Apr 2007
Location: United Kingdom
Posts: 1,330
Thanks: 0
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by scottjcampbell View Post
Sorry, this is ages ago, but my code:

PHP Code:

<?
include("include/session.php");
//IF FILE IS ALREADY MADE
if (file_exists($session->username))
{
//MAKE DIRECTOY
mkdir($session->username);
echo 
"made";
}
//OTHERWISE
else
{
//DONT
echo "already made";
}
?>
always comes up as "already made" even if its not, what am i doing wrong?
As you can see from the logic of the above, it seems to be wrong. it will only make a file if it exists. therefore, that is wrong. it should be
PHP Code:

<? 
include("include/session.php"); 
//note the ! before the file_exists
if(!file_exists($session->username)) 

mkdir($session->username); 
echo 
"made"

else 

echo 
"already made"

?>
Jay
__________________
Useful Tutorials
[ PHP Video-1-2-3 ] [ MySQL 1-2-3 ]
For any php function reference type

www.php.net/FunctionName
Reply With Quote
  #8 (permalink)  
Old 04-03-08, 07:12 PM
phpdoctor's Avatar
phpdoctor phpdoctor is offline
Code Guru
 
Join Date: Feb 2007
Location: New Zealand
Posts: 767
Thanks: 4
Thanked 2 Times in 2 Posts
Wow old thread ... i was loads of view on this one...
Yep your code just needed the ! symbal lol (hate when that happens)

Your code was saying: if file exists then create the dir.
Now with the code the Jay made he put the ! operator in to make it: if file does not exist then create the dir

Good Luck Mate,
Lex
__________________
01010000 01001000 01010000
Reply With Quote
  #9 (permalink)  
Old 04-04-08, 06:23 AM
scottjcampbell scottjcampbell is offline
New Member
 
Join Date: Apr 2008
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by Jay6390 View Post
As you can see from the logic of the above, it seems to be wrong. it will only make a file if it exists. therefore, that is wrong. it should be
PHP Code:

<? 
include("include/session.php"); 
//note the ! before the file_exists
if(!file_exists($session->username)) 

mkdir($session->username); 
echo 
"made"

else 

echo 
"already made"

?>
Jay
Thank you SOO much, i was up all night trying to crack this. that just proves how much common sense i actually have.

Thanks! Scott Campbell - administrator@campbellmember.com
Reply With Quote
  #10 (permalink)  
Old 04-04-08, 06:27 AM
Jay6390's Avatar
Jay6390 Jay6390 is offline
Code Master
 
Join Date: Apr 2007
Location: United Kingdom
Posts: 1,330
Thanks: 0
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by scottjcampbell View Post
Thank you SOO much, i was up all night trying to crack this. that just proves how much common sense i actually have.

Thanks! Scott Campbell - administrator@campbellmember.com
It's amazing how simple some errors are. best thing to do is comment code as you go. makes reading logic easier and will be beneficial in the long run

Jay
__________________
Useful Tutorials
[ PHP Video-1-2-3 ] [ MySQL 1-2-3 ]
For any php function reference type

www.php.net/FunctionName
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 3 (0 members and 3 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] check if directory exists lordy PHP 2 07-04-05 01:17 AM
Directory Create adubb PHP 2 01-23-05 10:17 PM
Need Epinions-lite system in PHP & MYSQL wali001 Job Offers & Assistance 4 01-12-04 06:02 AM
Declared Functions skipper23 PHP 4 12-17-03 10:06 AM
index page not showing up skipper23 PHP 3 12-15-03 01:10 PM


All times are GMT -5. The time now is 06:30 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.