php – 計算資料夾或路徑的容量大小

/**
 * 計算資料夾路徑的檔案大小
 * 例如 $result = CalcDirectorySize("C:AppServwwwexhibition/10/");
 * 若計算GB則使用如 echo $result / 1024 / 1024 / 1024 ;
 *
 * Calculate the full size of a directory
 *
 * @author      Jonas John
 * @version     0.2
 * @link        http://www.jonasjohn.de/snippets/php/dir-size.htm
 * @param       string   $DirectoryPath    Directory path
 */
function CalcDirectorySize($DirectoryPath) {

    // I reccomend using a normalize_path function here
    // to make sure $DirectoryPath contains an ending slash
    // (-> http://www.jonasjohn.de/snippets/php/normalize-path.htm)

    // To display a good looking size you can use a readable_filesize
    // function.
    // (-> http://www.jonasjohn.de/snippets/php/readable-filesize.htm)

    $Size = 0;

    $Dir = opendir($DirectoryPath);

    if (!$Dir)
        return -1;

    while (($File = readdir($Dir)) !== false) {

        // Skip file pointers
        if ($File[0] == '.') continue;

        // Go recursive down, or add the file size
        if (is_dir($DirectoryPath . $File))
            $Size += CalcDirectorySize($DirectoryPath . $File . DIRECTORY_SEPARATOR);
        else
            $Size += filesize($DirectoryPath . $File);
    }

    closedir($Dir);

    return $Size;
}

php – 複製目錄或資料夾 copy()

因為copy()只能複製檔案不能複製資料夾
查了一下php官網
看到有網友寫出了一個可以自動判斷,無論檔案或是資料夾,都能成功的複製過去
這樣的話就方便多了~

參考:http://php.net/manual/en/function.copy.php

<?php
    /**
     * Copy file or folder from source to destination, it can do
     * recursive copy as well and is very smart
     * It recursively creates the dest file or directory path if there weren't exists
     * Situtaions :
     * - Src:/home/test/file.txt ,Dst:/home/test/b ,Result:/home/test/b -> If source was file copy file.txt name with b as name to destination
     * - Src:/home/test/file.txt ,Dst:/home/test/b/ ,Result:/home/test/b/file.txt -> If source was file Creates b directory if does not exsits and copy file.txt into it
     * - Src:/home/test ,Dst:/home/ ,Result:/home/test/** -> If source was directory copy test directory and all of its content into dest
     * - Src:/home/test/ ,Dst:/home/ ,Result:/home/**-> if source was direcotry copy its content to dest
     * - Src:/home/test ,Dst:/home/test2 ,Result:/home/test2/** -> if source was directoy copy it and its content to dest with test2 as name
     * - Src:/home/test/ ,Dst:/home/test2 ,Result:->/home/test2/** if source was directoy copy it and its content to dest with test2 as name
     * @todo
     *     - Should have rollback technique so it can undo the copy when it wasn't successful
     *  - Auto destination technique should be possible to turn off
     *  - Supporting callback function
     *  - May prevent some issues on shared enviroments : http://us3.php.net/umask
     * @param $source //file or folder
     * @param $dest ///file or folder
     * @param $options //folderPermission,filePermission
     * @return boolean
     */
    function smartCopy($source, $dest, $options=array('folderPermission'=>0755,'filePermission'=>0755))
    {
        $result=false;

        if (is_file($source)) {
            if ($dest[strlen($dest)-1]=='/') {
                if (!file_exists($dest)) {
                    cmfcDirectory::makeAll($dest,$options['folderPermission'],true);
                }
                $__dest=$dest."/".basename($source);
            } else {
                $__dest=$dest;
            }
            $result=copy($source, $__dest);
            chmod($__dest,$options['filePermission']);

        } elseif(is_dir($source)) {
            if ($dest[strlen($dest)-1]=='/') {
                if ($source[strlen($source)-1]=='/') {
                    //Copy only contents
                } else {
                    //Change parent itself and its contents
                    $dest=$dest.basename($source);
                    @mkdir($dest);
                    chmod($dest,$options['filePermission']);
                }
            } else {
                if ($source[strlen($source)-1]=='/') {
                    //Copy parent directory with new name and all its content
                    @mkdir($dest,$options['folderPermission']);
                    chmod($dest,$options['filePermission']);
                } else {
                    //Copy parent directory with new name and all its content
                    @mkdir($dest,$options['folderPermission']);
                    chmod($dest,$options['filePermission']);
                }
            }

            $dirHandle=opendir($source);
            while($file=readdir($dirHandle))
            {
                if($file!="." && $file!="..")
                {
                     if(!is_dir($source."/".$file)) {
                        $__dest=$dest."/".$file;
                    } else {
                        $__dest=$dest."/".$file;
                    }
                    //echo "$source/$file ||| $__dest<br />";
                    $result=smartCopy($source."/".$file, $__dest, $options);
                }
            }
            closedir($dirHandle);

        } else {
            $result=false;
        }
        return $result;
    }
?>

php – codeigniter 配置

application
cache

config

放置你的設定檔,須要使用$config[]為陣列。例如custom.php我把需要定義的常數, $config[‘custom’][‘define’][‘FB_APPID’] = “FACEBOOK APP帳號”,之後再透過controller去define(‘FB_APPID’)。也就是說,config裡面指做放置的動作,實際define則交由controller
controllers

core

自訂核心。我在這裡建立MY_controller.php,MY_controller 繼承 CI_controller ,當MY_controller建構的時候,

自行定義全域會用到的model或是funciton或是class或是常數

往後須要全域的controller就全部繼承MY_controller。

也就是本來:

CI_controller <- 每個controller

現在改成

CI_controller <- MY_controller <- 每個controller

errors

helpers

標準PHP的自訂funciton,一些不需要歸類到class通通就放在這裡吧!有class的記得放到libraries
hooks
language

libraries

標準PHP的自訂class, 只有function 記得放在helpers
logs
models
third_party

views

顯示的頁面囉,不過view裡面的html是可以使用$this->load->view(‘top.php’);

$this->load->view(‘menu.php’);

這樣能方便把不同區塊的html組合起來

css
我自訂的。用來純放CSS的地方
plugin
通常放置jQuery的外掛、字型、PHP外掛照理說PHP的外掛都是class啦,應該要放在application/libraries底下,放在這邊的用意非常明顯,就是”別人寫的”,且備份系統的時候可以跳過的,有需要才會把外掛放進來,甚至放而不用,不是非常重要的。這時候我通常都會歸類到plubin/PHP底下  

 

system
user_guide