php文件操作函数

以下摘自destoon的文件操作函数

<?php
/*
    [Destoon B2B System] Copyright (c) 2008-2013 Destoon.COM
    This is NOT a freeware, use is subject to license.txt
*/
defined('IN_DESTOON') or exit('Access Denied');
//写文件
if(!function_exists('file_put_contents')) {
    define('FILE_APPEND', 8);
    function file_put_contents($file, $string, $append = '') {
        $mode = $append == '' ? 'wb' : 'ab';//wb只写打开或新建一个二进制文件;只允许写数据。ab追加
        $fp = @fopen($file, $mode) or exit("Can not open $file");
        flock($fp, LOCK_EX);//函数锁定要取得独占锁定(写入的程序)  LOCK_SH要取得共享锁定(读取的程序)
        $stringlen = @fwrite($fp, $string);
        flock($fp, LOCK_UN);//释放锁定
        @fclose($fp);
        return $stringlen;
    }
}

//返回文件类型
function file_ext($filename) {
    //strrchr返回指定字符串从左开始最后一个出现的位置
    return strtolower(trim(substr(strrchr($filename, '.'), 1)));
}

//过滤不规则文件名
function file_vname($name) {
    return str_replace(array(' ', '\\', '/', ':', '*', '?', '"', '<', '>', '|', "'", '$', '&', '%', '#', '@'), array('-', '', '', '', '', '', '', '', '', '', '', '', '', '', '', ''), $name);
}

//下载文件
function file_down($file, $filename = '', $data = '') {
    if(!$data && !is_file($file)) exit;
    $filename = $filename ? $filename : basename($file);//函数返回路径中的文件名部分。
    $filetype = file_ext($filename);
    $filesize = $data ? strlen($data) : filesize($file);
    ob_end_clean();//清空(擦除)缓冲区并关闭输出缓冲
    @set_time_limit(0);//设置执行时间无限制
    //防止缓存
    if(strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE') !== false) {
        header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
        header('Pragma: public');
    } else {
        header('Pragma: no-cache');
    }
    header('Expires: '.gmdate('D, d M Y H:i:s').' GMT');
    header('Content-Encoding: none');
    header('Content-Length: '.$filesize);
    header('Content-Disposition: attachment; filename='.$filename);
    header('Content-Type: '.$filetype);
    if($data) { echo $data; } else { readfile($file); }
    exit;
}


//列出目录下所有文件
function file_list($dir, $fs = array()) {
    $files = glob($dir.'/*');
    if(!is_array($files)) return $fs;
    foreach($files as $file) {
        if(is_dir($file)) {
            $fs = file_list($file, $fs);
        } else {
            $fs[] = $file;
        }
    }
    return $fs;
}


//文件复制
function file_copy($from, $to) {
    dir_create(dirname($to));//dirname返回路径中目录部分
    if(is_file($to) && DT_CHMOD) @chmod($to, DT_CHMOD); //改变文件模式
    if(strpos($from, DT_PATH) !== false) $from = str_replace(DT_PATH, DT_ROOT.'/', $from);
    if(@copy($from, $to)) {
        if(DT_CHMOD) @chmod($to, DT_CHMOD);
        return true;
    } else {
        return false;
    }
}

//写文件
function file_put($filename, $data) {
    dir_create(dirname($filename));    
    if(@$fp = fopen($filename, 'wb')) {
        flock($fp, LOCK_EX);
        $len = fwrite($fp, $data);
        flock($fp, LOCK_UN);
        fclose($fp);
        if(DT_CHMOD) @chmod($filename, DT_CHMOD);
        return $len;
    } else {
        return false;
    }
}


//获取文件内容
function file_get($filename) {
    return @file_get_contents($filename);
}


//删除文件
function file_del($filename) {
    if(DT_CHMOD) @chmod($filename, DT_CHMOD);
    return is_file($filename) ? @unlink($filename) : false;
}


//返回文件目录
function dir_path($dirpath) {
    $dirpath = str_replace('\\', '/', $dirpath);
    if(substr($dirpath, -1) != '/') $dirpath = $dirpath.'/';
    return $dirpath;
}


//创建文件目录
function dir_create($path) {
    if(is_dir($path)) return true;
    if(DT_CACHE != DT_ROOT.'/file/cache' && strpos($path, DT_CACHE) !== false) {
        $dir = str_replace(DT_CACHE.'/', '', $path);
        $dir = dir_path($dir);
        $temp = explode('/', $dir);
        $cur_dir = DT_CACHE.'/';
        $max = count($temp) - 1;
        for($i = 0; $i < $max; $i++) {
            $cur_dir .= $temp[$i].'/';
            if(is_dir($cur_dir)) continue;
            @mkdir($cur_dir);
            if(DT_CHMOD) @chmod($cur_dir, DT_CHMOD);
            if(!is_file($cur_dir.'/index.html') && !is_file($cur_dir.'/index.php')) file_copy(DT_ROOT.'/file/index.html', $cur_dir.'/index.html');
        }
    } else {
        $idx = strpos($path, '/file/') !== false ? true : false;
        $dir = str_replace(DT_ROOT.'/', '', $path);
        $dir = dir_path($dir);
        $temp = explode('/', $dir);
        $cur_dir = DT_ROOT.'/';
        $max = count($temp) - 1;
        for($i = 0; $i < $max; $i++) {
            $cur_dir .= $temp[$i].'/';
            if(is_dir($cur_dir)) continue;
            @mkdir($cur_dir);
            if(DT_CHMOD) @chmod($cur_dir, DT_CHMOD);
            if($idx && !is_file($cur_dir.'/index.html') && !is_file($cur_dir.'/index.php')) file_copy(DT_ROOT.'/file/index.html', $cur_dir.'/index.html');
        }
    }
    return is_dir($path);
}


//改变文件权限
function dir_chmod($dir, $mode = '', $require = 0) {
    if(!$require) $require = substr($dir, -1) == '*' ? 2 : 0;
    if($require) {
        if($require == 2) $dir = substr($dir, 0, -1);
        $dir = dir_path($dir);
        $list = glob($dir.'*');
        foreach($list as $v) {
            if(is_dir($v)) {
                dir_chmod($v, $mode, 1);
            } else {
                @chmod(basename($v), $mode);
            }
        }
    }
    if(is_dir($dir)) {
        @chmod($dir, $mode);
    } else {
        @chmod(basename($dir), $mode);
    }
}


//复制文件目录
function dir_copy($fromdir, $todir) {
    $fromdir = dir_path($fromdir);
    $todir = dir_path($todir);
    if(!is_dir($fromdir)) return false;
    if(!is_dir($todir)) dir_create($todir);
    $list = glob($fromdir.'*');
    foreach($list as $v) {
        $path = $todir.basename($v);
        if(is_file($path) && !is_writable($path)) {
            if(DT_CHMOD) @chmod($path, DT_CHMOD);
        }
        if(is_dir($v)) {
            dir_copy($v, $path);
        } else {
            @copy($v, $path);
            if(DT_CHMOD) @chmod($path, DT_CHMOD);
        }
    }
    return true;
}


//删除文件目录
function dir_delete($dir) {
    $dir = dir_path($dir);
    if(!is_dir($dir)) return false;
    $dirs = array(DT_ROOT.'/admin/', DT_ROOT.'/api/', DT_CACHE.'/', DT_ROOT.'/file/', DT_ROOT.'/include/', DT_ROOT.'/lang/', DT_ROOT.'/member/', DT_ROOT.'/module/', DT_ROOT.'/extend/', DT_ROOT.'/skin/', DT_ROOT.'/template/', DT_ROOT.'/wap/');
    if(substr($dir, 0, 1) == '.' || in_array($dir, $dirs)) die("Cannot Remove System DIR $dir ");
    $list = glob($dir.'*');
    if($list) {
        foreach($list as $v) {
            is_dir($v) ? dir_delete($v) : @unlink($v);//删除文件
        }
    }
    return @rmdir($dir);//删除空目录
}


//获取文件list($ext制定后缀)
function get_file($dir, $ext = '', $fs = array()) {
    $files = glob($dir.'/*');
    if(!is_array($files)) return $fs;
    foreach($files as $file) {
        if(is_dir($file)) {
            if(is_file($file.'/index.php') && is_file($file.'/config.inc.php')) continue;
            $fs = get_file($file, $ext, $fs);
        } else {
            if($ext) {
                if(preg_match("/\.($ext)$/i", $file)) $fs[] = $file;
            } else {
                $fs[] = $file;
            }
        }
    }
    return $fs;
}

function is_write($file) {
    if(DT_WIN) {
        if(substr($file, -1) == '/') {
            if(is_dir($file)) {
                $file = $file.'writeable-test.tmp';
                if(@$fp = fopen($file, 'a')) {
                    flock($fp, LOCK_EX);
                    fwrite($fp, 'OK');
                    flock($fp, LOCK_UN);
                    fclose($fp);
                    $tmp = file_get_contents($file);
                    unlink($file);
                    if($tmp == 'OK') return true;
                }
                return false;
            } else {
                dir_create($file);
                if(is_dir($file)) return is_write($file);
                return false;
            }
        } else {
            if(@$fp = fopen($file, 'a')) {
                fclose($fp);
                return true;
            }
            return false;
        }
    } else {
        return is_writeable($file);//判断指定的文件是否可写。
    }
}
?>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

kitt15

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值