Yii Framework Forum: CFileHelper::initialize($dir) - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

CFileHelper::initialize($dir) Recursive Create Path (if not exists) Rate Topic: -----

#1 User is offline   saegeek 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 245
  • Joined: 09-December 09
  • Location:Montpellier - France

Posted 04 May 2012 - 12:16 AM

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.
And [for] their saying, "Indeed, we have killed the Messiah, Jesus, the son of Mary, the messenger of God ." And they did not kill him, nor did they crucify him; but [another] was made to resemble him to them. And indeed, those who differ over it are in doubt about it. They have no knowledge of it except the following of assumption. And they did not kill him, for certain.Rather, God raised him to Himself.
0

#2 User is offline   samdark 

  • Having fun
  • Yii
  • Group: Yii Dev Team
  • Posts: 3,788
  • Joined: 17-January 09
  • Location:Russia

Posted 04 May 2012 - 01:16 AM

http://php.net/manua...ction.mkdir.php, see $recursive.
Yii 1.1 Application Development Cookbook

Enjoying Yii? Star us at github: 1.1 and 2.0.
0

#3 User is offline   saegeek 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 245
  • Joined: 09-December 09
  • Location:Montpellier - France

Posted 04 May 2012 - 02:03 AM

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:

Quote

CAssetManager.basePath "/var/www/assets" is invalid. Please make sure the directory exists and is writable by the Web server process.

And [for] their saying, "Indeed, we have killed the Messiah, Jesus, the son of Mary, the messenger of God ." And they did not kill him, nor did they crucify him; but [another] was made to resemble him to them. And indeed, those who differ over it are in doubt about it. They have no knowledge of it except the following of assumption. And they did not kill him, for certain.Rather, God raised him to Himself.
0

#4 User is offline   samdark 

  • Having fun
  • Yii
  • Group: Yii Dev Team
  • Posts: 3,788
  • Joined: 17-January 09
  • Location:Russia

Posted 04 May 2012 - 02:05 AM

No, it should not because it's one time action and checking it every time will consume resources.
Yii 1.1 Application Development Cookbook

Enjoying Yii? Star us at github: 1.1 and 2.0.
0

#5 User is offline   saegeek 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 245
  • Joined: 09-December 09
  • Location:Montpellier - France

Posted 04 May 2012 - 05:03 AM

View Postsamdark, on 04 May 2012 - 02:05 AM, said:

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. ));
}

And [for] their saying, "Indeed, we have killed the Messiah, Jesus, the son of Mary, the messenger of God ." And they did not kill him, nor did they crucify him; but [another] was made to resemble him to them. And indeed, those who differ over it are in doubt about it. They have no knowledge of it except the following of assumption. And they did not kill him, for certain.Rather, God raised him to Himself.
0

#6 User is offline   Maurizio Domba Cerin 

  • Yii - Yesss It Is !!!
  • Yii
  • Group: Yii Dev Team
  • Posts: 4,359
  • Joined: 12-October 09
  • Location:Croatia

Posted 05 May 2012 - 01:34 AM

You are getting this error because you delete a vital folder of your application, that should not be done in the first place.
Find more about me.... btw. Do you know your WAN IP?
0

#7 User is offline   saegeek 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 245
  • Joined: 09-December 09
  • Location:Montpellier - France

Posted 24 May 2012 - 02:06 PM

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.
And [for] their saying, "Indeed, we have killed the Messiah, Jesus, the son of Mary, the messenger of God ." And they did not kill him, nor did they crucify him; but [another] was made to resemble him to them. And indeed, those who differ over it are in doubt about it. They have no knowledge of it except the following of assumption. And they did not kill him, for certain.Rather, God raised him to Himself.
0

#8 User is offline   Maurizio Domba Cerin 

  • Yii - Yesss It Is !!!
  • Yii
  • Group: Yii Dev Team
  • Posts: 4,359
  • Joined: 12-October 09
  • Location:Croatia

Posted 24 May 2012 - 03:09 PM

It's common usage to create an empty file in this kind of folders so they do not get deleted.
Find more about me.... btw. Do you know your WAN IP?
0

#9 User is offline   jacmoe 

  • Elite Member
  • Yii
  • Group: Moderators
  • Posts: 2,601
  • Joined: 10-October 10
  • Location:Denmark

Posted 24 May 2012 - 03:53 PM

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.
"Less noise - more signal"
0

#10 User is offline   dhimes 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 15
  • Joined: 07-August 12

Posted 07 August 2012 - 12:48 PM

View Postjacmoe, on 24 May 2012 - 03:53 PM, said:

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
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users