1
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

phpディレクトリ作成

Last updated at Posted at 2017-01-11

phpディレクトリ作成

ディレクトリがある場合は何もせず、無い場合は作成する処理。(入れ子構造対応)

public function mkdir($path, $mod= 0777) {
    if (file_exists($path))
        return TRUE;
    if (@mkdir($path, $mod, TRUE)==FALSE)
        return FALSE;
    chmod($path, $mod);
    if (file_exists($path))
        return TRUE;
    return FALSE;
}

mkdirの箇所は権限関係で書き込めない場合のエラーを@でエラー表示をしないようにしている。
is_writeble関数で下記のように権限チェックも考えたが、再帰的にディレクトリを作成する際のチェックも面倒なので無し。

    if (is_writable($xxxx)==FALSE)
        return FALSE;

また、mkdirの第二引数の権限付与はumaskの影響を受けるため、直後にchmodで対応している。

備考

php7.0以降で書く場合は、引数・戻り値ともに型指定ができるので下記になる。

public function mkdir(string $path, int $mod= 0777) :bool {}

以上メモ。

1
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?