12

I have a PHP Script that creates a folder based on a form. I'm wondering if there is a way to NOt create and replace that folder if it already exists?

<?php 
mkdir("QuickLinks/$_POST[contractno]");
?>
2
  • What are you trying to achieve exactly? What does your program do? Commented Jul 12, 2012 at 15:27
  • The program creates a folder based on input from a form, and then copies a template folder into the new folder. I'm wondering if there is also a way to tell the script that if the folder is already there; don't copy anything? It a in-house project, password is needed to access the web-page. Commented Jul 12, 2012 at 15:29

7 Answers 7

17

You can use is_dir:

<?php 
$path = "QuickLinks/$_POST[contractno]";
if(!is_dir($path)){
  mkdir($path);
}
?>
Sign up to request clarification or add additional context in comments.

Comments

6

In general:

$dirname = "whatever";
if (!is_dir($dirname)) {
    mkdir($dirname);
}

In particular: be very careful when doing filesystem (or any other type of sensitive) operations that involve user input! The current example (create a directory) doesn't leave much of an open attack surface, but validating the input can never hurt.

Comments

4

Use is_dir to check if folder exists

$dir = "/my/path/to/dir";
if (!is_dir($dir)) {
    if (false === @mkdir($dir, 0777, true)) {
        throw new \RuntimeException(sprintf('Unable to create the %s directory', $dir));
    }
}

Attention to the uncontrolled input, it is very dangerous!

Comments

2

You can try:

<?php 
    if (!is_dir("QuickLinks/$_POST[contractno]"))
        mkdir("QuickLinks/$_POST[contractno]");
?>

2 Comments

This seems to have worked. Is there a way to do this for copying a folder? If the folder already exists then don't copy anything?
Yeah, this should work: <?php if (!is_dir($dest_dir)) shell_exec("cp -r $src_dir $dest_dir"); ?>. If there is problem with shell_exec() function, replace it with some other copying function inspired from this topic.
1

you can take a look at :

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

Comments

1

Use the is_dir-function of PHP to check if there is already a directory and call the mkdir-function only if there isn't one.

Comments

0

Do some validation rules (regexp) here before using POST variable to create the directory !

if(!file_exists("QuickLinks/$_POST[contractno]"))
    mkdir("QuickLinks/$_POST[contractno]");

3 Comments

file_exists doesn't differentiate between files and directories, so a file with the name you are looking for will produce a false true. is_dir checks if it exists AND if it is a directory
@hellsgate: Which is the better approach in such cases. You can't create the directory if a file or named socket under that same name exists. So is_dir sounds like the more exact approach, but can lead to false negatives and additional errors. Purely fictional. But the fallback code should be invoked for existing files and directories.
@mario: Good point. For this scenario, I now realise file_exists is the better option because of the reasons you list.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.