zoukankan      html  css  js  c++  java
  • PHP文件相关函数试题

    一.问答题

    1.返回路径中的文件名部分的函数是什么?

    2.改变文件模式的函数是什么?

    3.拷贝文件的函数是什么?

    4.返回路径中的目录部分的函数是什么?

    5.将上传的文件移动到指定位置的函数是? 

    6.返回规范化的绝对路径名的函数是什么?

    二.编程题

    1.请用三种方法写出函数,获取某个目录下所有文件和文件夹名的关联数组,要求给函数传一个路径参数,返回一个数组,格式为:array('dir'=>array('dir1','dir2'...),'file'=>array())

    2.封装一个系统文件类,类中方法包括:判断文件是否存在,获取文件内容(包括锁和不锁文件),输入内容到一个文件,追加内容到文件,删除文件,移动文件,拷贝文件,文件的文件名部分,文件的目录部分,文件的后缀名部分,文件的mine类型,文件的大小,获取文件上一次修改时间,判断是否为路径,判断文件是否可写,判断是否为文件,创建文件夹,复制一个目录,删除一个目录。


    答案

    一.问答题

    1.string basename ( string $path [, string $suffix ] )

    2.bool chmod ( string $filename , int $mode )

    3.bool copy ( string $source , string $dest [, resource $context ] )

    4.string dirname ( string $path )

    5.bool move_uploaded_file ( string $filename , string $destination )

    6.string realpath ( string $path ) 

    二.编程题

    1

    function scanDir1($dir = './'){
    
        $result['dir'] = $result['file'] = array();
    
        if(!is_dir($dir)) return $result;
    
        foreach (scandir($dir) as $df) {
            if($df==='.'||$df==='..') continue;
            if(is_dir($dir.'/'.$df)) $result['dir'][] = $df;
            if(is_file($dir.'/'.$df)) $result['file'][] = $df;
        }
    
        return $result;
    }
    
    function scanDir2($dir = './'){
    
        $result['dir'] = $result['file'] = array();
    
        if(!is_dir($dir)) return $result;
    
        $handle = dir($dir);
        while (($df = $handle -> read()) !== false) {
            if($df==='.'||$df==='..') continue;
            if(is_dir($dir.'/'.$df)) $result['dir'][] = $df;
            if(is_file($dir.'/'.$df)) $result['file'][] = $df;
        }
    
        $handle -> close();
        return $result;
    }
    
    function scanDir3($dir = './'){
    
        $result['dir'] = $result['file'] = array();
    
        if(!is_dir($dir)) return $result;
    
        $handle = opendir($dir);
        while (($df = readdir($handle)) !== false) {
            if($df==='.'||$df==='..') continue;
            if(is_dir($dir.'/'.$df)) $result['dir'][] = $df;
            if(is_file($dir.'/'.$df)) $result['file'][] = $df;
        }
    
        return $result;
    }

    2.

    <?php
    
    class FileSystem{
    
        public function exists($path)
        {
            return file_exists($path);
        }
    
        public function isFile($path)
        {
            return is_file($path);
        }
    
        public function isDir($path)
        {
            return is_dir($path);
        }
    
        public function get($path,$lock = false)
        {
            if(!$this->exists($path)) return '';
            if($lock){
                return $this -> lockGet($path);
            }else{
                return file_get_contents($path);
            }
        }
    
        private function lockGet($path)
        {
            $contents = '';
            $handle = fopen($path, 'r');
            if($handle){
                try {
                    if(flock($handle, LOCK_SH)){
                        while (!feof($handle)) {
                            $contents .= fread($handle, 1048576);
                        }
                    }
                } finally {
                    fclose($handle);
                }
            }
            return $contents;
        }
    
        public function put($path,$contents,$lock = false)
        {
            return file_put_contents($path,$contents,$lock?LOCK_SH:0);
        }
    
        public function append($path,$contents)
        {
            return file_put_contents($path,$contents,FILE_APPEND);
        }
    
        public function delete($path)
        {
            if($this->isFile($path)){
                return unlink($path);
            }else if($this->isDir($path)){
                foreach (scandir($path) as $df) {
                    if($df!=='.'||$df!=='..'){
                        $this->delete($path.'/'.$df);
                    }
                }
                rmdir($path);
            }
        }
    
        public function move($path,$target)
        {
            return rename($path,$target);
        }
    
        public function copy($path,$target)
        {
            return copy($path,$target);
        }
    
        public function name($path)
        {
            return pathinfo($path,PATHINFO_FILENAME);
        }
    
        public function dirname($path)
        {
            return pathinfo($path,PATHINFO_DIRNAME);
        }
    
        public function extension($path)
        {
            return pathinfo($path,PATHINFO_EXTENSION);
        }
    
        public function type($path)
        {
            return filetype($path);
        }
    
        public function size($path)
        {
            return filesize($path);
        }
    
        public function lastModified($path)
        {
            return filemtime($path);
        }
    
        public function isWritable($path)
        {
            return is_writable($path);
        }
    
        public function makeDir($path,$mode = 0755)
        {
            return @mkdir($path,$mode);
        }
    }
  • 相关阅读:
    centos8 将SSSD配置为使用LDAP并要求TLS身份验证
    Centos8 搭建 kafka2.8 .net5 简单使用kafka
    .net core 3.1 ActionFilter 拦截器 偶然 OnActionExecuting 中HttpContext.Session.Id 为空字符串 的问题
    Springboot根据不同环境加载对应的配置
    VMware Workstation12 安装 Centos8.3
    .net core json配置文件小结
    springboot mybatisplus createtime和updatetime自动填充
    .net core autofac依赖注入简洁版
    .Net Core 使用 redis 存储 session
    .Net Core 接入 RocketMQ
  • 原文地址:https://www.cnblogs.com/xiaozong/p/5750921.html
Copyright © 2011-2022 走看看