Warning: mkdir(): File exists [closed]

Posted on

Warning: mkdir(): File exists [closed] – Here in this article, we will share some of the most common and frequently asked about PHP problem in programming with detailed answers and code samples. There’s nothing quite so frustrating as being faced with PHP errors and being unable to figure out what is preventing your website from functioning as it should like php and . If you have an existing PHP-based website or application that is experiencing performance issues, let’s get thinking about Warning: mkdir(): File exists [closed].

The file transferring to my upload folder is working well but I have a warning in mkdir. It says file exist but the picture and folder generates own name. I don’t know what warning is determining.

include 'connect.php';

$dir = substr(uniqid(), -7); // Uniqid for subdirectory

$path = "uploads/$dir/"; // uploads/subdirectory/  // Make directory

$valid_formats = array("jpg", "png", "jpeg", "kml");

$max_file_size = 2097152;

$count = 0;

// Loop $_FILES to execute all files

if (!empty($_FILES)) {
    foreach ($_FILES['files']['name'] as $f => $name) {
        if ($_FILES['files']['error'][$f] == 4) {
            continue; // Skip file if any error found

        if ($_FILES['files']['error'][$f] == 0) {
            if ($_FILES['files']['size'][$f] > $max_file_size) {
                $message[] = "$name is too large!.";
                continue; // Skip large files
            } elseif (!in_array(pathinfo($name, PATHINFO_EXTENSION), $valid_formats)) {
                $message[] = "$name is not a valid format";
                continue; // Skip invalid file formats
            } else { // No error found! Move uploaded files
                mkdir($path, 0700);
                $ext = pathinfo($_FILES['files']['name'][$f], PATHINFO_EXTENSION);
                $uniq_name = substr(uniqid(), -5) . '.' . $ext;
                $dest = $path . $uniq_name;

                if (move_uploaded_file($_FILES["files"]["tmp_name"][$f], $dest)) {
                    // more logic

Solution :

Warning is quite clear, you are creating directory which already exists. So, just change it to

if (!file_exists($path)) {
    mkdir($path, 0700);

Use PHP’s is_dir($path_to_dir) for checking if a directory exists from before.

Official documentation

Leave a Reply

Your email address will not be published. Required fields are marked *