0

I just wanna ask, what is the code needed in my code in order this warning will not show

Warning: mkdir() [function.mkdir]: File exists in C:\xampp\htdocs\php-robert\dir\dir.php

also did my program is correct? what i want in my program is, if the folder is not exist, make a folder, if its existing just do nothing.. nothing to show, just nothing

dir.php

<?php
$var = "MyFolder";
$structure = "../../file/rep/$var";

if (!mkdir($structure, 0700)) {

}
else
{
echo"folder created";
}

?>
4
  • why not check to see if it exists first? file_exists is the function, I believe. Commented Nov 14, 2012 at 3:22
  • The other good one to use is is_dir() Commented Nov 14, 2012 at 3:23
  • "File exists" what could that mean ? Commented Nov 14, 2012 at 3:24
  • @Dagon Think File exists warning is always show when the folder is existing Commented Nov 14, 2012 at 3:26

3 Answers 3

2

Try the following:

$folder = "folder_name";
// if folder does not exist or the name is used, just not for a folder
if (!file_exists($folder) || !is_dir($folder)) {
    if (mkdir($folder, 0755)) {
        echo 'Folder created';
    } else {
        echo 'Unable to create folder';
    }
}
Sign up to request clarification or add additional context in comments.

4 Comments

!file_exists($folder) meaning of this NOT EXISTING?
Yes, that means: [ if the file/folder does NOT exist ]. It does not know how to differentiate between folders and files however, that is why the second clause in the IF statement is used.
Sir Burmat file/folder because of this file_exists(for the file) and this for the folder is_dir thx sir
file_exists() works for both files AND directories. Read the documentation for more information: php.net/manual/en/function.file-exists.php . Also, the 0755 represents the directory permissions for the directory you are creating. 0755 will apply the following to your directory: rwxr-xr-x. Check the documentation for mkdir for more information also: php.net/manual/en/function.mkdir.php
1
if (!is_dir($structure)) {
  mkdir($structure);
}
else
{
  echo "folder already exists";
}

2 Comments

Parse error: syntax error, unexpected '{' in C:\xampp\htdocs\php-robert\dir\dir.php
missing a closing bracket before the first brace
1
if (is_dir($structure) == false and mkdir($structure, 0700) == false)
{
  echo "error creating folder";
}
else
{
  echo "folder exists or was created";
}

You could also test if a file exists, but it isn't a folder

2 Comments

its working but can u explain this line is_dir($structure) == false and !mkdir($structure, 0700) thx
First the directory is checked to see that it doesn't exist, then mkdir() is executed, however if mkdir() returns false (ie failure) then it will echo the error in the first statement. otherwise it continues to say that the directory either existed (when is_dir() first returns true) or mkdir didn't fail (meaning the directory was created). Basically the control structure skips trying to make the directory if it already exists, because if the first part fails, the second isn't executed in PHP due to the "AND"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.