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
原创粉丝点击