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]");
?>
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]");
?>
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.
You can try:
<?php
if (!is_dir("QuickLinks/$_POST[contractno]"))
mkdir("QuickLinks/$_POST[contractno]");
?>
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.
Do some validation rules (regexp) here before using POST variable to create the directory !
if(!file_exists("QuickLinks/$_POST[contractno]"))
mkdir("QuickLinks/$_POST[contractno]");