CFileHelper::initialize($dir)

Hi,

I just finished a new function that create recursively a path (eg. an upload directory) if it not exists.

I think this is very useful and should be integrated into the CFileHelper.

So we will obtain: CFileHelper::initialize($path);




    public static function initialize($dir) {

        if(file_exists($dir)||empty($dir)) 

            return false;

        $array=$this->_dirList($dir);

        foreach($array as $value) {

            if(file_exists($value)) 

                continue;

            mkdir($value);

        }

            return true;

    }

    private function _dirList($dir) {

        // remove ending slash

        $dir=(substr($dir,-1)==DIRECTORY_SEPARATOR?substr($dir,0,-1):$dir);

        

        $isUnix=($dir[0]==DIRECTORY_SEPARATOR?true:false);

        $e=explode(DIRECTORY_SEPARATOR,$dir);

        $c=count($e);

        $list=array();

        if($isUnix) {

            $list[0]=DIRECTORY_SEPARATOR;

            for($i=0;$i!=$c;$i++) {

                if(empty($e[$i])) 

                    continue;

                $list[$i]=$list[$i-1].$e[$i].DIRECTORY_SEPARATOR;

            }

        }

        else {

            for($i=0;$i!=$c;$i++) {

                if($i==0) 

                    $list[$i]=$e[$i].DIRECTORY_SEPARATOR;

                else 

                    $list[$i]=$list[$i-1].$e[$i].DIRECTORY_SEPARATOR;

            }

        }

        return $list;

    }



the function _dirList() is required by initialize().

Enjoy.

http://php.net/manual/en/function.mkdir.php, see $recursive.

oops I did not know it.

OK, but at least this behavior should be set by default when Yii tries to access a path (create the path before the CException).

Example:

When i rename or delete the "assets" folder i obtain:

No, it should not because it’s one time action and checking it every time will consume resources.

It will not consume ressource because it only occurs when a directory is not found.

something like:




try {

mkdir(...);

}

catch {

CEXception(yii:t(CAssetManager.basePath "/var/www/assets" is invalid. Please make sure the directory exists and is writable by the Web server process. ));

}



You are getting this error because you delete a vital folder of your application, that should not be done in the first place.

But this is done by Mercurial (versioning prog) (all the empty folders are deleted).

Mercurial considers that the application should be as clean as possible and auto-create its required folders on the fly.

It’s common usage to create an empty file in this kind of folders so they do not get deleted.

I create a .yii file in assets and protected/runtime. That will teach Mercurial to keep them. ;)

Provided that you add those two files to your repository, of course.

Aha! I came here searching for the answer to my problem




"the asset /public_root/trackstar/yiiroot/framework/zii/widgets/assets to be published does not exist"



when pulling from the hg repo. Are those the only two places I need dummy files? (I’m learning by going through “Agile Web App Development with Yii1.1 and php5”

Also, what does your hgignore file look like? Mine looks like the following (use vi editor), where the syntax: regexp portion was created by yii:




syntax: glob

*~

*.swp

.*.swp

hgpush.sh

hgpull.sh

*.ogv

*.mp4

.~*#

*.log

build/

dist/

logs/


syntax: regexp

# ignore all except .hgkeep

assets/(?!.*\.hgkeep$).+

protected/runtime/(?!.*\.hgkeep$).+



Thanks a ton!

Dan