php Warning「mkdir(): File exists in xxx」が発生した場合の対処法

  • 作成日 2022.06.08
  • php
php Warning「mkdir(): File exists in xxx」が発生した場合の対処法

phpで、Warning「mkdir(): File exists in xxx」が発生した場合の対処法を記述してます。phpのバージョンは8.1です。

環境

  • OS  windows 11 home 64bit
  • php 8.1.2

Warning全文

以下のコードで発生。

<?php

$path = "./dir";

mkdir( $path, true );

原因

作成しようとしているディレクトリが既に存在するため

対処法

存在確認を行ってから、作成する

<?php

$path = "./dir";

if(!is_dir($path)){
    mkdir( $path, true );
}