|
Posted by Carl Pearson on 01/12/07 16:11
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.
Navigation:
[Reply to this message]
|