Creating a folder when I run file_put_contents()

后端 未结 4 646
情歌与酒
情歌与酒 2020-12-02 18:06

I have uploaded a lot of images from the website, and need to organize files in a better way. Therefore, I decide to create a folder by months.

$month  = dat         


        
4条回答
  •  鱼传尺愫
    2020-12-02 18:40

    I wrote a function you might like. It is called forceDir(). It basicaly checks whether the dir you want exists. If so, it does nothing. If not, it will create the directory. A reason to use this function, instead of just mkdir, is that this function can create nexted folders as well.. For example ('upload/promotions/januari/firstHalfOfTheMonth'). Just add the path to the desired dir_path.

    function forceDir($dir){
        if(!is_dir($dir)){
            $dir_p = explode('/',$dir);
            for($a = 1 ; $a <= count($dir_p) ; $a++){
                @mkdir(implode('/',array_slice($dir_p,0,$a)));  
            }
        }
    }
    

提交回复
热议问题