Home > php教程 > PHP源码 > body text

php目录操作类

PHP中文网
Release: 2016-05-25 17:06:40
Original
1045 people have browsed it

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;
		};
	}	
}
Copy after login

                               

                   

Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template