How do I check if a directory exists? “is_dir”, “file_exists” or both?

后端 未结 11 1604
礼貌的吻别
礼貌的吻别 2020-11-28 18:36

I want to create a directory if it does\'nt exist already.

Is using is_dir enough for that purpose?

if ( !is_dir( $dir ) ) {
    mkdir(          


        
11条回答
  •  挽巷
    挽巷 (楼主)
    2020-11-28 19:37

    $dirname = $_POST["search"];
    $filename = "/folder/" . $dirname . "/";
    
    if (!file_exists($filename)) {
        mkdir("folder/" . $dirname, 0777);
        echo "The directory $dirname was successfully created.";
        exit;
    } else {
        echo "The directory $dirname exists.";
    }
    

提交回复
热议问题