目录操作:

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

  

PHP - 目录、文件操作的更多相关文章

  1. PHP7语法知识(四):目录文件操作、Cookie与Session、MySQL数据库的使用、Redis数据库、PHP处理XML与JSON

    目录文件操作 一.目录 1.判断文件类型: 2.创建和删除目录: 3.打开读取和关闭目录 4.获得路径中目录部分 5.目录磁盘空间 二.文件操作 1.打开文件: 2.读取文件: 3.获得文件属性: 4 ...

  2. python目录/文件操作

    目录操作 sys.argv[0] # 获得当前脚本路径,即当前工作目录\脚本名 os.getcwd() # 获得当前工作目录 os.path.abspath('.') # 获得当前工作目录 os.pa ...

  3. linux目录文件操作

    一.linux系统目录结构 1.顶层根目录 顶层根目录使用 “/”来表示 2.linux中的一些重要目录 (1)bin目录 放置常用的可执行文件(其中ls命令位列其中) (2)sbin目录 放置系统的 ...

  4. 每天一个linux命令(目录文件操作):【转载】Linux 目录结构

    对于每一个Linux学习者来说,了解Linux文件系统的目录结构,是学好Linux的至关重要的一步.,深入了解linux文件目录结构的标准和每个目录的详细功能,对于我们用好linux系统至关重要,下面 ...

  5. 人生苦短_我用Python_OS对目录/文件操作_005

    # coding=utf-8 import os # 操作文件和目录 ", os.getcwd()) # 获取当前文件的目录 ", os.path.realpath(__file_ ...

  6. python之目录文件操作

    [1.os] 1.重命名:os.rename(old, new) 2.删除:os.remove(file) 3.列出目录下的文件 :os.listdir(path) 4.获取当前工作目录:os.get ...

  7. iOS沙盒目录文件操作

    简介 沙盒(NSHomeDirectory())中总共有四个文件夹,documents.tmp.app.Library; 手动保存的文件在documents文件里; Nsuserdefaults保存的 ...

  8. 每天一个linux命令(目录文件操作):【转载】linux文件属性详解

    Linux 文件或目录的属性主要包括:文件或目录的节点.种类.权限模式.链接数量.所归属的用户和用户组.最近访问或修改的时间等内容.具体情况如下: 命令:  ls -lih 输出: [root@loc ...

  9. linux之目录文件操作

  10. [C/C++][文件操作] 对比目录并列出同名较新文件、较旧文件 0.1

    主要是模仿robocopy的部分功能 (robocopy /L 参数可以列出本地目录和备份目录中的异同之处,主要是标记出:较新的.较旧的.多出的文件 ) 现在还不会写GUI,打算后面自己做目录树dif ...

随机推荐

  1. .NET中的IO操作之文件流(一)

    读操作 //1.创建文件流 FileStream fsRead =new FileStream("1.txt",FileMode.Open); //2.创建缓冲区,正常情况下,是不 ...

  2. javascript 未结束的字符串常量

    1.JAVASCRIPT引用时,使用的字符语言不一致. 比如:<script type=”text/javascript” src=”xxx.js” charset=”UTF-8″>.xx ...

  3. Java "double字符串转数字"

    1.int 表示数字的简单类型(值类型),double 表示数字的双精度类型(值类型),  而Integer和Double类型是一个引用的复杂类型 2.Integer.valueOf(String s ...

  4. GoWithTheFlow

    GoWithTheFlow http://notes.jetienne.com/2011/07/17/gowiththeflow.js-async-flow-control-with-a-zen-to ...

  5. QT小技巧—更好管理项目(增加预编译头文件,并且指定moc文件的生成位置)good

    预编译加速编译 QT也可以像VS那样使用预编译头文件来加速编译器的编译速度.首先在.pro文件中加入: CONFIG += precompiled_header 然后定义需要预编译的头文件: PREC ...

  6. Nginx工作原理和优化、漏洞(转)

    查看安装了哪些模块命令: [root@RG-PowerCache-X xcache]# nginx/sbin/nginx -Vnginx version: nginx/1.2.3built by gc ...

  7. linux 的 ping 原理

    ping命令的工作原理是: ping命令是用来查看网络上另一个主机系统的网络连接是否正常的一个工具. 他向网络上的另一个主机系统发送ICMP报文,如果指定系统得到了报文,它将把报文原样传回给发送者,这 ...

  8. 谁有SMI-S Provider的一些源码,能参考一下吗

    我要用OpenPegasus根据SMI-S规范来写provider,不知道如何下手啊,求高手指点

  9. 你应当知道的Java牛人

    Java领域有非常多著名的人物,他们为Java社区编写框架.产品.工具或撰写书籍改变了Java编程的方式. 本文是<最受欢迎的8位Java牛人>的2.0版本号. PS:排名不分先后.本文的 ...

  10. java--折半查找

    /* 折半查找 */ class TwoSearch { //折半查找可以提高效率,但必须得保证是有序的数组 public static int halfSearch(int[] arr,int ke ...