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 ...
随机推荐
- 转-——推荐几个web中常用的一些js图表插件 - zccst
http://www.tuicool.com/articles/bqq2Qn 作者:zccst 我自己用过fusioncharts和highchart. jQuery插件有: TufteGraph f ...
- 五子棋Web版的开发(二)--整合Spring4.3+hibernate4+Struts2.3
拖了这么久才把ssh框架给整合完毕,期间发现自己对SSH的知识真的是知之甚少.在整合期间遇到了无数的坑,我还是先把项目地址发一下吧 首先我遇到的第一个问题是 CreateQuery is not va ...
- .net DataTable 取值辅助类
DataTableCommon类主要是帮助取值 方法列表: public static string GetCellString(DataTable dt,int row, int column) p ...
- IOS 特定于设备的开发:Info.plist属性列表的设置
应用程序的Info.plist属性列表使你能够在向iTunes提交应用程序时指定应用程序的要求.这些限制允许告诉iTunes应用程序需要哪些设备特性. 每个IOS单元都会提供一个独特的特性集.一些设备 ...
- 阿里云ECS每天一件事D3:挂载硬盘
阿里云的系统盘通常都不大,对于我们的日常使用,基本不足,因此都会额外购买至少一块硬盘,作为存储数据之用. 数据盘要经过分区.格式化.挂载三个步骤,方能正常使用. 1.数据盘的分区 先使用fdisk命 ...
- CPU卡
CPU卡芯片通俗地讲就是指芯片内含有一个微处理器,它的功能相当于一台微型计算机.人们经常使用的集成电路卡(IC卡)上的金属片就是CPU卡芯片.CPU卡可适用于金融.保险.交警.政府行业等多个领域,具有 ...
- C#共享内存类改进版
原文 C#共享内存类改进版 改进说明及源码实例下载见:http://blog.csdn.net/zzh8845/archive/2008/11/22/3349963.aspx ShareMem.cs ...
- HDU 1507 Uncle Tom's Inherited Land*
题目大意:给你一个矩形,然后输入矩形里面池塘的坐标(不能放东西的地方),问可以放的地方中,最多可以放多少块1*2的长方形方块,并输出那些方块的位置. 题解:我们将所有未被覆盖的分为两种,即分为黑白格( ...
- 1005 - Rooks(规律)
1005 - Rooks PDF (English) Statistics Forum Time Limit: 1 second(s) Memory Limit: 32 MB A rook is ...
- Ext JS学习第十三天 Ext基础之 Ext.Element
•Ext.Element提供了181个方法,嗯,还没完,只是在4.1版本中是这样,最新的4.2版本貌似又增加了新方法,可谓是相当丰富给力.那么根据操作类型基本可以分为查询系.DOM操作系.样式操作系. ...