Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP mkdir() Function - If Folder Exists [duplicate]

Tags:

forms

php

Possible Duplicate:
PHP uploading script - create folder automatically

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]");
?>
like image 892
ItsJoeTurner Avatar asked Jul 12 '12 15:07

ItsJoeTurner


2 Answers

You can use is_dir:

<?php 
$path = "QuickLinks/$_POST[contractno]";
if(!is_dir($path)){
  mkdir($path);
}
?>
like image 77
Zbigniew Avatar answered Oct 03 '22 03:10

Zbigniew


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.

like image 38
Jon Avatar answered Oct 03 '22 03:10

Jon