php建立多級目錄完整封裝類操作

來源:互聯網
上載者:User

標籤:fputs   屬性   baidu   服務   目錄   touch   and   log   time   

建立多級目錄函式中調用建立指定下的指定檔案的函數:

 public function create_dir($dir,$mode=0777)    {        return is_dir($dir) or ($this->create_dir(dirname($dir)) and mkdir($dir, $mode));    }

建立指定路徑下的指定檔案,string $path(需要包含檔案名稱和尾碼),boolean $over_write 是否覆蓋檔案,int $time 設定時間。預設是當前系統時間,int $atime 設定訪問時間。預設是當前系統時間。函數如下中調用了create_dir建立目錄函式及刪除unlink_file函數:

 public function create_file($path,$over_write=FALSE,$time=NULL,$atime=NULL)    {        $path = $this->dir_replace($path);        $time = empty($time) ? time() : $time;        $atime = empty($atime) ? time() : $atime;        if(file_exists($path) && $over_write)        {            $this->unlink_file($path);        }        $aimDir = dirname($path);        $this->create_dir($aimDir);        return touch($path,$time,$atime);    }

刪除非空目錄函式,說明:只能刪除非系統和特定許可權的檔案,否則會出現錯誤,string$dirName目錄路徑,boolean$is_all是否刪除所有,boolean $del_dir 是否刪除目錄,函數如下:

public function remove_dir($dir_path,$is_all=FALSE)    {        $dirName = $this->dir_replace($dir_path);        $handle = @opendir($dirName);        while (($file = @readdir($handle)) !== FALSE)        {            if($file != ‘.‘ && $file != ‘..‘)            {                $dir = $dirName . ‘/‘ . $file;                if($is_all)                {                    is_dir($dir) ? $this->remove_dir($dir) : $this->unlink_file($dir);                }                else                 {                    if(is_file($dir))                    {                        $this->unlink_file($dir);                    }                }            }        }        closedir($handle);        return @rmdir($dirName);    }

 替換路徑中//的字元相應的字元函數:

 public function dir_replace($path)    {        return str_replace(‘//‘,‘/‘,str_replace(‘\\‘,‘/‘,$path));    }
 /**     * 指定檔案編碼轉換     * @param string $path 檔案路徑     * @param string $input_code 原始編碼     * @param string $out_code 輸出編碼     * @return boolean     */    public function change_file_code($path,$input_code,$out_code)    {        if(is_file($path))//檢查檔案是否存在,如果存在就執行轉碼,返回真        {            $content = file_get_contents($path);            $content = string::chang_code($content,$input_code,$out_code);            $fp = fopen($path,‘w‘);            return fputs($fp,$content) ? TRUE : FALSE;            fclose($fp);        }    }

/**
* 取得上傳檔案資訊
* @param $file file屬性資訊
* @return array
*/

 public function get_upload_file_info($file)    {        $file_info = $_FILES[$file];//取得上傳檔案基本資料        $info = array();        $info[‘type‘]  = strtolower(trim(stripslashes(preg_replace("/^(.+?);.*$/", "\\1", $file_info[‘type‘])), ‘"‘));//取得檔案類型        $info[‘temp‘]  = $file_info[‘tmp_name‘];//取得上傳檔案在伺服器中臨時儲存目錄        $info[‘size‘]  = $file_info[‘size‘];//取得上傳檔案大小        $info[‘error‘] = $file_info[‘error‘];//取得檔案上傳錯誤        $info[‘name‘]  = $file_info[‘name‘];//取得上傳檔案名稱        $info[‘ext‘]   = $this->get_ext($file_info[‘name‘]);//取得上傳檔案尾碼        return $info;    }

/**
* 取得檔案路徑資訊
* @param $full_path 完整路徑
* @return ArrayObject
*/

 public function get_file_type($path)    {        //pathinfo() 函數以數組的形式返迴文件路徑的資訊。        //---------$file_info = pathinfo($path); echo file_info[‘extension‘];----------//        //extension取得檔案尾碼名【pathinfo($path,PATHINFO_EXTENSION)】-----dirname取得檔案路徑【pathinfo($path,PATHINFO_DIRNAME)】-----basename取得檔案完整檔案名稱【pathinfo($path,PATHINFO_BASENAME)】-----filename取得檔案名稱【pathinfo($path,PATHINFO_FILENAME)】        return pathinfo($path);    }

/**
* 返回指定檔案和目錄的資訊
* @param string $file
* @return ArrayObject
*/

public function list_info($file)    {        $dir = array();        $dir[‘filename‘]   = basename($file);//返迴路徑中的檔案名稱部分。        $dir[‘pathname‘]   = realpath($file);//返回絕對路徑名。        $dir[‘owner‘]      = fileowner($file);//檔案的 user ID (所有者)。        $dir[‘perms‘]      = fileperms($file);//返迴文件的 inode 編號。        $dir[‘inode‘]      = fileinode($file);//返迴文件的 inode 編號。        $dir[‘group‘]      = filegroup($file);//返迴文件的組 ID。        $dir[‘path‘]       = dirname($file);//返迴路徑中的目錄名稱部分。        $dir[‘atime‘]      = fileatime($file);//返迴文件的上次訪問時間。        $dir[‘ctime‘]      = filectime($file);//返迴文件的上次改變時間。        $dir[‘perms‘]      = fileperms($file);//返迴文件的許可權。         $dir[‘size‘]       = filesize($file);//返迴文件大小。        $dir[‘type‘]       = filetype($file);//返迴文件類型。        $dir[‘ext‘]        = is_file($file) ? pathinfo($file,PATHINFO_EXTENSION) : ‘‘;//返迴文件尾碼名        $dir[‘mtime‘]      = filemtime($file);//返迴文件的上次修改時間。        $dir[‘isDir‘]      = is_dir($file);//判斷指定的檔案名稱是否是一個目錄。        $dir[‘isFile‘]     = is_file($file);//判斷指定檔案是否為常規的檔案。        $dir[‘isLink‘]     = is_link($file);//判斷指定的檔案是否是串連。        $dir[‘isReadable‘] = is_readable($file);//判斷檔案是否可讀。        $dir[‘isWritable‘] = is_writable($file);//判斷檔案是否可寫。        $dir[‘isUpload‘]   = is_uploaded_file($file);//判斷檔案是否是通過 HTTP POST 上傳的。        return $dir;    }

完整封裝代碼內容有限,請到下面去下載:http://pan.baidu.com/s/1gf4AGoF

密碼:iqun

php建立多級目錄完整封裝類操作

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.