目录操作:

<?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. Android应用开发提高篇(1)-----获取本地IP

    链接地址:http://www.cnblogs.com/lknlfy/archive/2012/02/21/2361802.html 一.概述 习惯了Linux下的网络编程,在还没用智能机之前就一直想 ...

  2. centos6.5 升级python 到 python 2.7.11 安装 pip

    1.首先官方下载源码,然后安装(./configure,make all,make install,make clean,make distclean) 注意:需要先安装zlib-devel,open ...

  3. hdu 4614 Vases and Flowers 线段树

    题目链接 一共n个盒子, 两种操作, 第一种是给出两个数x, y, 从第x个盒子开始放y朵花, 一个盒子只能放一朵, 如果某个盒子已经有了, 那么就跳过这个盒子放下面的盒子. 直到花放完了或者到了最后 ...

  4. HTML+CSS笔记 CSS进阶续集

    元素分类 在CSS中,html中的标签元素大体被分为三种不同的类型:块状元素.内联元素(又叫行内元素)和内联块状元素. 常用的块状元素有: <div>.<p>.<h1&g ...

  5. javascript Node操作

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  6. .net mvc Authorization Filter,Exception Filter与Action Filter

    一:知识点部分 权限是做网页经常要涉及到的一个知识点,在使用MVC做权限设计时需要先了解以下知识: MVC中Url的执行是按照Controller->Action->View页面,但是我们 ...

  7. Windows10 上运行Ubuntu Bash

    Windows10 上运行Ubuntu Bash 2016年4月6日,Windows 10 Insider Preview 发布的版本 14316,添加了Ubuntu Bash,在Windows上提供 ...

  8. 我的Fedora环境

    Fedora现在也更新到了第20个版本,只是在15+以后的版本,大多数操作,都是大同小异的,也不必特意去关注版本号,只有对应到具体的软件,可能会因为库的版本,有或多或少的区别. 之前每次都喜欢按照一些 ...

  9. Spring RESTful服务接收和返回JSON最佳实践

    http://blog.csdn.net/prince_hua/article/details/12103501

  10. PROTEL99生成GERBER的操作说明

    GBL BOTTOM LAYER(底层布线图)GBO BOTTOM OVERLAYER(底层丝印层)GBP BOTTOM PASTE LAYER(底层锡膏层)GBS BOTTOM SOLDER MAS ...