PHP 文件操作
来源:互联网 发布:vb.net catia二次开发 编辑:程序博客网 时间:2024/04/30 13:16
php 文件操作
创建文件/文件夹
创建文件
/* 方法一 */$file = fopen("test.txt", "w");/* 方法二 */touch($file);
创建文件夹
mkdir("/web/www/testing/test_dir", 0700);
创建多层文件夹
/* 原生 */mkdir("/web/www/testing/test_dir", 0700, true);/* 递归 */function mkdirs($path) { return is_dir($path) or (mkdirs(dirname($path)) and mkdir($path,0777));}
创建文件并写入内容
/* 方法一 */function write_file($path, $data, $mode = FOPEN_WRITE_CREATE_DESTRUCTIVE) { if ( ! $fp = @fopen($path, $mode)) { return FALSE; } flock($fp, LOCK_EX); fwrite($fp, $data); flock($fp, LOCK_UN); fclose($fp); return TRUE; }/* 方法二 */file_put_contents($file, $data);
删除文件/文件夹
删除文件
unlink('test.txt');
删除文件夹
rmdir('test_dir')
删除文件夹及内容
/* 递归 */function delete_dir($path, $del_dir = FALSE, $level = 0) { $path = rtrim($path, DIRECTORY_SEPARATOR); if (!$current_dir = @opendir($path)) { return FALSE; } while (FALSE !== ($filename = @readdir($current_dir))) { if ($filename != "." and $filename != "..") { if (is_dir($path.DIRECTORY_SEPARATOR.$filename)) { if (substr($filename, 0, 1) != '.') { delete_dir($path.DIRECTORY_SEPARATOR.$filename, $del_dir, $level + 1); } } else { unlink($path.DIRECTORY_SEPARATOR.$filename); } } } @closedir($current_dir); if ($del_dir == TRUE AND $level > 0) { return @rmdir($path); } return TRUE; }/* 调用系统命令 */function delete_dir($path) { if(strtoupper(substr(PHP_OS, 0, 3)) == 'WIN') { $str = "rmdir /s/q " . $path; } else { $str = "rm -Rf " . $path; } system($str); }
修改文件/文件夹
修改文件及文件夹名称
rename($old_name, $new_name);
移动文件及文件夹
rename($old_path, $new_path);
修改文件及文件夹权限
chmod($file, $mode);
修改文件及文件夹所有者
chown($file, $user);
修改文件及文件夹的修改和访问时间
touch($file, $time, $atime);
查询文件/文件夹
查询文件夹目录
function get_filenames($source_dir, $include_path = FALSE, $_recursion = FALSE){ static $_filedata = array(); if ($fp = @opendir($source_dir)) { if ($_recursion === FALSE) { $_filedata = array(); $source_dir = rtrim(realpath($source_dir), DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR; } while (FALSE !== ($file = readdir($fp))) { if (@is_dir($source_dir.$file) && strncmp($file, '.', 1) !== 0) { get_filenames($source_dir.$file.DIRECTORY_SEPARATOR, $include_path, TRUE); } elseif (strncmp($file, '.', 1) !== 0) { $_filedata[] = ($include_path == TRUE) ? $source_dir.$file : $file; } } return $_filedata; } else { return FALSE; } }
获取文件内容
function read_file($file) { if ( ! file_exists($file)) { return FALSE; } if (function_exists('file_get_contents')) { return file_get_contents($file); } if (!$fp = @fopen($file, FOPEN_READ)) { return FALSE; } flock($fp, LOCK_SH); $data = ''; if (filesize($file) > 0) { $data =& fread($fp, filesize($file)); } flock($fp, LOCK_UN); fclose($fp); return $data;}
查询文件及文件夹信息
/* 获取文件上次访问时间 */fileatime($file);/* 获取文件大小 */filesize($file);/* 获取文件所有者 */fileowner($file);/* 获取文件权限 */fileperms($file);/* 获取文件上次修改时间 */filemtime($file);
文件及文件夹相关函数
判断是否存在
file_exists($file);
判断权限
/* 是否可读 */is_readable($file);/* 是否可写 */is_writable($file);/* 是否可执行 */is_executable($file);
0 0
- php操作php文件
- 【php】目录操作 文件操作
- PHP操作文件问答
- PHP文件操作类
- PHP操作文件问答
- php文件操作
- PHP文件操作
- php写文件操作
- php文件操作
- PHP 文件读写操作
- php 操作csv文件
- PHP文件操作
- PHP读写文件操作
- php文件上传操作
- php 文件操作
- php 文件操作1
- php 文件操作
- PHP学习文件操作
- Application详解(一)
- leetcode: Excel Sheet Column Number
- C++宏定义详解
- Windows7 64bit 如何在odbc数据源中添加access驱动的问题
- 大陆争霸sdoi2010
- PHP 文件操作
- html5权威指南—读后感
- stl源码剖析 详细学习笔记deque(2)
- 松鼠聚会2013天津市选
- Loader异步装载器-笔记
- HBase安装过程遇到的问题
- [转]基于mysql数据库binlog的增量订阅&消费中间件:Canal
- Python3.4 远程操控电脑(开关机)
- 链表中设置头结点的作用