首頁  >  文章  >  php教程  >  php目录操作类

php目录操作类

PHP中文网
PHP中文网原創
2016-05-25 17:06:40997瀏覽

1. [文件]     Dir.class.php

<?php
class Dir{
	private $_dir;
	/**
	 * 目录类
	 * @author 李俊[duguying2008@gmail.com]
	 * @param string $dir 目录
	 */
	function __construct($dir) {
		$this->_dir=$dir;
	}
	/**
	 * 计算目录大小
	 * @param string $dir 目录
	 * @return number 字节
	 */
	public function dirSize($dir=null){
		if ($dir==null) {
			$dir=$this->_dir;
		}
		if (!is_string($dir)) {
			throw new Exception(&#39;目录名必须为string类型!&#39;);
		}
		$size=0;
		$items=scandir($dir);
		foreach ($items as $item) {
			if (is_file($dir.&#39;/&#39;.$item)) {
				$size=$size+filesize($dir.&#39;/&#39;.$item);
			}elseif (is_dir($dir.&#39;/&#39;.$item)&&&#39;.&#39;!=$item&&&#39;..&#39;!=$item){
				$size=$size+$this->dirSize($dir.&#39;/&#39;.$item);
			}
		}
		return $size;
	}
	/**
	 * 判断文件或目录可读
	 * @author 李俊
	 * @param string $dir 目录名
	 * @return bool
	 */
	function readable($dir=null) {
		if ($dir==null) {
			$dir=$this->_dir;
		}
		if (!is_string($dir)) {
			throw new Exception(&#39;目录名必须为string类型!&#39;);
		}
		if (($frst=file_get_contents($dir))&&is_file($dir)) {
			return true;//是文件,并且可读
		}else {//是目录
			if (is_dir($dir)&&scandir($dir)) {
				return true;//目录可读
			}else {
				return false;
			}
		}
	}
	/**
	 * 判断文件或目录是否可写
	 * @author 李俊
	 * @param string $dir 目录名
	 * @return bool
	 */
	function writeable($dir=null) {
		if ($dir==null) {
			$dir=$this->_dir;
		}
		if (!is_string($dir)) {
			throw new Exception(&#39;目录名必须为string类型!&#39;);
		}
		if (is_file($dir)) {//对文件的判断
			return is_writeable($dir);
		}elseif (is_dir($dir)) {
			//开始写入测试;
			$file=&#39;_______&#39;.time().rand().&#39;_______&#39;;
			$file=$dir.&#39;/&#39;.$file;
			if (file_put_contents($file, &#39;//&#39;)) {
				unlink($file);//删除测试文件
				return true;
			}else {
				return false;
			}
		}else {
			return false;
		};
	}	
}

                               

                   

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn