PHP - 目录、文件操作

时间:2022-09-30 17:04:19

目录操作:

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