473,404 Members | 2,187 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,404 software developers and data experts.

About directory

Hello everyone, I'm a beginner in PHP and I want to ask about
directory. How to check correctly if the directory already exist in the
webroot? I've tried with this syntax:
....
bool $state = mkdir( "test" );
if ( $state == false ) chdir( "test" );
....
but on the page there is a warning. How to solve this problem? Thanks
in advance.

Jan 11 '07 #1
5 1347
..oO(roby)
>Hello everyone, I'm a beginner in PHP and I want to ask about
directory. How to check correctly if the directory already exist in the
webroot?
http://www.php.net/file_exists

Micha
Jan 11 '07 #2

roby wrote:
Hello everyone, I'm a beginner in PHP and I want to ask about
directory. How to check correctly if the directory already exist in the
webroot? I've tried with this syntax:
...
bool $state = mkdir( "test" );
if ( $state == false ) chdir( "test" );
...
but on the page there is a warning. How to solve this problem? Thanks
in advance.
Or you could do something like this if you don't need the state
recorded...

<?

if(is_dir("test")){
chdir("test");
}

?>

Jan 11 '07 #3

roby wrote:
Hello everyone, I'm a beginner in PHP and I want to ask about
directory. How to check correctly if the directory already exist in the
webroot? I've tried with this syntax:
...
bool $state = mkdir( "test" );
if ( $state == false ) chdir( "test" );
...
but on the page there is a warning. How to solve this problem? Thanks
in advance.
Try using the function is_dir() to test if a dir exists and is of type
directory.

http://www.php.net/manual/en/function.is-dir.php

<?php
mkdir("test");
if(is_dir("test")){
chdir("test");
}
?>

Jan 11 '07 #4
Thanks for the help... it works.
Noodle wrote:
roby wrote:
Hello everyone, I'm a beginner in PHP and I want to ask about
directory. How to check correctly if the directory already exist in the
webroot? I've tried with this syntax:
...
bool $state = mkdir( "test" );
if ( $state == false ) chdir( "test" );
...
but on the page there is a warning. How to solve this problem? Thanks
in advance.

Try using the function is_dir() to test if a dir exists and is of type
directory.

http://www.php.net/manual/en/function.is-dir.php

<?php
mkdir("test");
if(is_dir("test")){
chdir("test");
}
?>
Jan 12 '07 #5
roby wrote:
Hello everyone, I'm a beginner in PHP and I want to ask about
directory. How to check correctly if the directory already exist in the
webroot? I've tried with this syntax:
...
bool $state = mkdir( "test" );
if ( $state == false ) chdir( "test" );
...
but on the page there is a warning. How to solve this problem? Thanks
in advance.
As already mentioned, is_dir might be a better way to handle such tests.

A couple of things, though. One, that logic seems suspect. Is the
warning perhaps one of not being able to change the directory? Your
code as written seems to only try to change dirs if it was *NOT* made.

Also, PHP is pretty smart. You don't always need to type variables
(though if you want to, it doesn't hurt anything).

// A slight re-working of your code.
//
// Will automatically return true or false based on mkdir success.
$state = mkdir("test");

// This will actually fail, as "false" will mean the
// directory was NOT created...
if (!$state)
{
chdir("test");
}

// Still incorrect logic, but here is your code all in one statement...
if (!mkdir("test"))
{
chdir("test");
}
// You could also have put it on one line...
if (!mkdir("test")) chdir("test");

I personally like the brackets, they *do* take up a tiny amount more
code but provide a good visual for the logic. Your mileage may vary.
Jan 12 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Herman | last post by:
Hi everyone, I recently installed the Sun J2SE SDK on my machine, and I am having trouble running the java.exe interpreter for my Java compiled code. I remember that I had to set my environment...
1
by: Tony Johansson | last post by:
Hello I'm trying to learn XML by reading a book. There is some text in the book that I don't understand so I try to ask you out there. My first question is does this statement mean that ...
3
by: jm | last post by:
I have a question about codebehind. I have an upload namespace, class and all in a codebehind. I currently have it so it works with one of my .aspx pages. I want to use it for another .aspx...
7
by: Bennett Haselton | last post by:
If you create a ASP.Net Web application in Visual Studio .Net, you apparently have to specify the target as a directory on a web server, e.g. www.hostname.com/dirname, with "dirname" being the name...
14
by: Alan Silver | last post by:
Hello, I have spent ages trawling through Google, looking for information about global functions in ASP.NET and I'm still not clear about the best way to go about this (or not). I am writing...
3
by: needin4mation | last post by:
Hi, had a question about event hooks. I don't know that much about it so please forgive me if I don't use all the right words. Is it possible to use a hook that listens or watches a certain...
2
by: Diffident | last post by:
Hello All, I just finished reading an interesting article by Scott about App Domains: http://odetocode.com/Articles/305.aspx Scott, I have a question about the section "Shadow Copies and...
90
by: John Salerno | last post by:
I'm a little confused. Why doesn't s evaluate to True in the first part, but it does in the second? Is the first statement something different? False print 'hi' hi Thanks.
63
by: David Mathog | last post by:
There have been a series of questions about directory operations, all of which have been answered with "there is no portable way to do this". This raises the perfectly reasonable question, why,...
2
by: Joey | last post by:
I have written an app in C#/asp.net 2.0 that is a system built to handle a large number of scenarios. Part of that system involves allowing users to download large files. As part of my original...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.