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 ...
随机推荐
- HDU2018-母牛的故事
描述: 有一头母牛,它每年年初生一头小母牛.每头小母牛从第四个年头开始,每年年初也生一头小母牛.请编程实现在第n年的时候,共有多少头母牛? 代码: 第n年的牛,等于第n-1年的牛(已有的)+第n-3年 ...
- 使得fiddler来抓包查看微信浏览器的网页源码
需要工具:http://www.telerik.com/fiddler 下载安装后 第二步: 打开这个选项: 设置代理:allow remote computer to connect 端口为888 ...
- codeforces 522D. Closest Equals 线段树+离线
题目链接 n个数m个询问, 每次询问输出给定区间中任意两个相同的数的最近距离. 先将询问读进来, 然后按r从小到大排序, 将n个数按顺序插入, 并用map统计之前是否出现过, 如果出现过, 就更新线段 ...
- python的reduce()函数
reduce()函数也是Python内置的一个高阶函数. reduce()函数接收的参数和 map()类似,一个函数 f,一个list,但行为和 map()不同,reduce()传入的函数 f 必须接 ...
- windows如何安装scrapy
第一次写博客,有不好的地方请理解! 在linux下安装scrapy几行命令就搞定了,windows就是事多! 话不多说,我们直接进入主题: 1. 下载python.地址 https://www.pyt ...
- QT太多的内容和模块,怎么办?
我有个问题,QT可以做许多不同的开源项目,而且每个QT新版本都那么内容,感觉学不过来.用不过来那么我们还应该学习和使用其它语言吗? 如果回答,在需要的时候学习,那么这句话意味着,这几年你基本上就局限于 ...
- poj 2001 Shortest Prefixes(字典树)
题目链接:http://poj.org/problem?id=2001 思路分析: 在Trie结点中添加数据域childNum,表示以该字符串为前缀的字符数目: 在创建结点时,路径上的所有除叶子节点以 ...
- HttpWebRequest和WebClient的区别
HttpWebRequest和WebClient的区别(From Linzheng): 1,HttpWebRequest是个抽象类,所以无法new的,需要调用HttpWebRequest.Creat ...
- SQL Server调试常用参数
SQL Server 清除缓存: DBCC DROPCLEANBUFFERS 从缓冲池中删除所有清除缓冲区. DBCC FREEPROCCACHE 从过程缓存中删除所有元素. DBCC FREESYS ...
- 关于移动端 rem 布局的一些总结
[资源一]基础知识恕不回顾 基础知识参考以下两篇博客: http://isux.tencent.com/web-app-rem.html http://www.w3cplus.com/css3/def ...