• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

PHP-目录、文件操作

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

目录操作:

<?php
/**
 * Read Directory.
 * Just read the top-level directory.
 * @param  string $path directory 
 *                      Eg:readDirectory( 'file/' );
 * @return array  directory array
 */
function readDirectory( $path ) {
	//open directory handle.
	$dirHanlde = opendir( $path );
	//Loop through directory.
	while ( ( $item = readdir( $dirHanlde ) ) !== FALSE ) {
		$p = $path . '/' . $item;
		if ( $item == '.' || $item == '..' ) continue;
		if ( is_file( $p ) ) $arr['file'][] = $item;
		if ( is_dir( $p ) )  $arr['dir'][] = $item;
	}
	//close directory handle.
	closedir( $dirHanlde );
	//return result.
	return $arr;
}

/**
 * calculate directory size.
 * @param  string directory size.
 * @return $num
 */
function dirSize( $path ) {
	static $num;
	$openHandle = opendir( $path );
	while ( ( $item = readdir( $openHandle ) ) !== FALSE ) {
		$p = $path . '/' . $item;
		if ( $item == '.' || $item == '..' ) continue;
		if ( is_file( $p ) ) $num += filesize( $p );
		if ( is_dir( $p ) ) dirSize( $p );
	}
	closedir( $openHandle );
	return $num;
}

/**
 * copy directory
 * 				Eg:
 * 					- copyDir( 'file/', 'abc/file' );
 * 					- copyDir( 'file/a', 'abc/def/a' );
 * @return [type] [description]
 */
function copyDir( $src, $dst ) {
	if ( ! file_exists( $dst ) ) mkdir( $dst, 0777, true );
	$openHandle = opendir( $src );
	while ( ( $item = readdir( $openHandle ) ) !== FALSE ) {
		$srcpath = $src . '/' . $item; 
		$dstpath = $dst . '/' . $item; 
		if ( $item == '.' || $item == '..' ) continue;
		if ( is_file( $srcpath ) ) copy( $srcpath, $dstpath );
		if ( is_dir( $srcpath ) ) copyDir( $srcpath, $dstpath );
	}
	closedir( $openHandle );
	return true;
}

  

文件操作:

<?php
/**
 * Conversion Bytes.
 * 					Eg:transBytes( 500000 );
 * @param  int $size Bytes.
 * @return float return value. 
 */
function transBytes( $size ) {
	$byteUnits = 0;
	$arr = array( 'B', 'KB', 'MB', 'GB', 'TB', 'EB');
	while ( $size >= 1024 ) {
		$size /= 1024;
		$byteUnits++;
	}
	return round( $size, 2 ) . $arr[ $byteUnits ];
}

/**
 * create file. include path.
 * 							Eg:createFile( 'file/abc.txt' );
 * @param  string $filename filename(include path.).
 * @return string result message.
 */
function createFile( $filename ) {
	//check file.
 	if ( checkFile( $filename ) ) {
	 	//create file.
	 	if ( @touch( $filename ) ) {
	 		return 'Success!';
	 	} else { return 'Failed to create file.'; }
 	} else { return 'check file faild!'; }
}

/**
 * rename file. 
 * 				Eg:renameFile( 'file/lllllssss.txt', 'file/abc.txt' );
 * @param  string $oldname old file name.
 * @param  string $newname new file name.
 * @return result.
 */
function renameFile( $oldname, $newname ) {
	//check file.
 	if ( checkFile( $newname ) ) {
 		//create file.
	 	if ( @rename( $oldname, $newname ) ) {
	 		return 'Success!';
	 	} else { return 'Failed to rename file.'; }
 	} else { return 'check file faild!'; }
}

/**
 * delete file.
 * 			Eg:delFile( 'file/lllll.html' );
 * @param  string file path.
 * @return return message.
 */
function delFile( $name ) {	
	( @unlink( $name ) ) ? $resultMsg = 'Delete Success!' : $resultMsg = 'Delete Faild!';
	return $resultMsg;
}

/**
 * download file.
 * 				Eg:downFile( 'file/abc.html' );
 * @param  string filename
 * @return download file name
 */
function downFile( $filename ) {
	header( 'Content-Disposition:attachment;filename=' . basename( $filename ) );
	header( 'Content-length:' . filesize( $filename ) );
	readfile( $filename );
}

/**
 * check file.
 * 			Eg:checkFile( $name );
 * include:
 * 	- Function checkFileName()
 * 	- Function checkFileExists()
 * @param  string $name file name.
 * @return bool
 */
function checkFile( $name ) {
	//verify the file name.
	if ( checkFileName( $name ) ) {
		//To determine whether the file already exists
		 if ( checkFileExists( $name ) ) {
		 	return true;
		} else { return false; }
	} else { return false; }
}

/**
 * check file name.
 * 					Eg:checkFileName( 'abc.txt' );
 * @param  string $name file name.
 * @return bool   success return true,faild return false.
 */
function checkFileName( $name ) {
	//Regular Expressions.
	//very that the file name is valid.
	$pattern = "/[\/,\*,<>,\?\|]/";
	//verify the file name.
	//return
	return @preg_match( $pattern, basename( $name ) );
}

/**
 * check file exists.
 * inlcude path.
 * 				Eg:checkFileExists( 'nnn.txt' );
 * @param  string $name file name.
 * @return bool   success return true,faild return false.
 */
function checkFileExists( $name ) {
	return @file_exists( $name );
}

  

公共操作:

<?php
/**
 * Dispaly information and jump pages.
 * @param  string $msg   display information.
 * @param  string $path  jump path.
 * @return This function has no return value.
 */
function alertMsg( $msg, $path ) {
	echo "<script type='text/javascript'>alert('" . $msg . "');location.href = '" . $path . "';</script>";
}

  


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
php获取当前时间发布时间:2022-07-10
下一篇:
PHP模拟登陆发布时间:2022-07-10
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap