PHP - 目录、文件操作
目录操作:
<?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 - 目录、文件操作的更多相关文章
- PHP7语法知识(四):目录文件操作、Cookie与Session、MySQL数据库的使用、Redis数据库、PHP处理XML与JSON
目录文件操作 一.目录 1.判断文件类型: 2.创建和删除目录: 3.打开读取和关闭目录 4.获得路径中目录部分 5.目录磁盘空间 二.文件操作 1.打开文件: 2.读取文件: 3.获得文件属性: 4 ...
- python目录/文件操作
目录操作 sys.argv[0] # 获得当前脚本路径,即当前工作目录\脚本名 os.getcwd() # 获得当前工作目录 os.path.abspath('.') # 获得当前工作目录 os.pa ...
- linux目录文件操作
一.linux系统目录结构 1.顶层根目录 顶层根目录使用 “/”来表示 2.linux中的一些重要目录 (1)bin目录 放置常用的可执行文件(其中ls命令位列其中) (2)sbin目录 放置系统的 ...
- 每天一个linux命令(目录文件操作):【转载】Linux 目录结构
对于每一个Linux学习者来说,了解Linux文件系统的目录结构,是学好Linux的至关重要的一步.,深入了解linux文件目录结构的标准和每个目录的详细功能,对于我们用好linux系统至关重要,下面 ...
- 人生苦短_我用Python_OS对目录/文件操作_005
# coding=utf-8 import os # 操作文件和目录 ", os.getcwd()) # 获取当前文件的目录 ", os.path.realpath(__file_ ...
- python之目录文件操作
[1.os] 1.重命名:os.rename(old, new) 2.删除:os.remove(file) 3.列出目录下的文件 :os.listdir(path) 4.获取当前工作目录:os.get ...
- iOS沙盒目录文件操作
简介 沙盒(NSHomeDirectory())中总共有四个文件夹,documents.tmp.app.Library; 手动保存的文件在documents文件里; Nsuserdefaults保存的 ...
- 每天一个linux命令(目录文件操作):【转载】linux文件属性详解
Linux 文件或目录的属性主要包括:文件或目录的节点.种类.权限模式.链接数量.所归属的用户和用户组.最近访问或修改的时间等内容.具体情况如下: 命令: ls -lih 输出: [root@loc ...
- linux之目录文件操作
- [C/C++][文件操作] 对比目录并列出同名较新文件、较旧文件 0.1
主要是模仿robocopy的部分功能 (robocopy /L 参数可以列出本地目录和备份目录中的异同之处,主要是标记出:较新的.较旧的.多出的文件 ) 现在还不会写GUI,打算后面自己做目录树dif ...
随机推荐
- gcc 的编译过程
通常我们都是使用下面的命令来直接生成可执行文件 gcc demo.c -o demo 对于我们来说十分简单,但是对编译器来说却完成了一系列复杂的工作,概括起来有如下几步: 1. 预处理 gcc -E ...
- 重定向输入输出流--freopen
freopen是被包含于C标准库头文件<stdio.h>中的一个函数,用于重定向输入输出流.该函数可以在不改变代码原貌的情况下改变输入输出环境. C99函数声明: FILE *freope ...
- Java学习之对象实例化
一个对象实例化过程:Person p = new Person();1,JVM会读取指定的路径下的Person.class文件,并加载进内存,并会先加载Person的父类(如果有直接的父类的情况下). ...
- LINQ的用法
http://www.cnblogs.com/liulun/archive/2013/02/26/2909985.html(转载)
- phplib系统开发经验总结
数据库设计: 数据库的设计一定要在了解整个系统需求的情况下,把数据库设计,及ER图画出来,数据库字典也要及时把握,只有掌握了这些才能下手开始设计界面,后期如果有需要,可以在数据库中添加数据,但要及时更 ...
- 简明的例子讲解position:relative、float、overflow:hidden和inline-block
标签(空格分隔): css relative float 我们通过一个简单的实验来了解position:relative float overflow:hidden 和 inline-block. 下 ...
- xp的停止更新对我们有什么影响?
微软与2001年推出windows xp系统,这款系统的成功毋庸置疑,但由于太过成功,微软在随后推出的vista系统和win7系统普及起来却异常困难.大多数人已经习惯了xp的操作,再加上一批铁杆旧电脑 ...
- python下读取excel文件
项目中要用到这个,所以记录一下. python下读取excel文件方法多种,用的是普通的xlrd插件,因为它各种版本的excel文件都可读. 首先在https://pypi.python.org/py ...
- 新唐M0特点分析
1,价格低,05x系列0.6-1.5美金,1xx系列1.5-3.5美金:2,性能好,最新32位CORTEX-M0的ARM核,唯一可工作到+5.5V的CORTEX-M0:3,速度快,CPU核能跑到50M ...
- A Brief Introduction to Multiset[STL]
基础 multiset是<set>库中一个非常有用的类型,它可以看成一个序列,插入一个数,删除一个数都能够在O(logn)的时间内完成,而且他能时刻保证序列中的数是有序的,而且序列中可以存 ...