Heim  >  Artikel  >  php教程  >  php目录操作类

php目录操作类

PHP中文网
PHP中文网Original
2016-05-25 17:06:40999Durchsuche

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

                               

                   

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn