11

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]");
?>
Dharman
  • 30,962
  • 25
  • 85
  • 135
ItsJoeTurner
  • 497
  • 2
  • 7
  • 24
  • What are you trying to achieve exactly? What does your program do? – Madara's Ghost Jul 12 '12 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. – ItsJoeTurner Jul 12 '12 at 15:29

7 Answers7

16

You can use is_dir:

<?php 
$path = "QuickLinks/$_POST[contractno]";
if(!is_dir($path)){
  mkdir($path);
}
?>
Zbigniew
  • 27,184
  • 6
  • 59
  • 66
5

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.

Jon
  • 428,835
  • 81
  • 738
  • 806
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!

Federkun
  • 36,084
  • 8
  • 78
  • 90
2

You can try:

<?php 
    if (!is_dir("QuickLinks/$_POST[contractno]"))
        mkdir("QuickLinks/$_POST[contractno]");
?>
cek-cek
  • 470
  • 8
  • 13
  • 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? – ItsJoeTurner Jul 12 '12 at 15:28
  • Yeah, this should work: ``. If there is problem with shell_exec() function, replace it with some other copying function inspired from [this](http://stackoverflow.com/questions/2050859/copy-entire-contents-of-a-directory-to-another-using-php) topic. – cek-cek Jul 12 '12 at 15:37
1

you can take a look at :

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

Damien Locque
  • 1,810
  • 2
  • 20
  • 42
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.

oktopus
  • 1,739
  • 1
  • 11
  • 12
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]");
Anthony
  • 12,177
  • 9
  • 69
  • 105
Benoit
  • 680
  • 1
  • 6
  • 17
  • 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 Jul 12 '12 at 15:14
  • 1
    @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 Jul 12 '12 at 15:19
  • @mario: Good point. For this scenario, I now realise file_exists is the better option because of the reasons you list. – hellsgate Jul 12 '12 at 15:24