一.问答题

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);
}
}

PHP文件相关函数试题的更多相关文章

  1. Django+七牛上传+查看+下载文件相关函数,新整理未完全测试

    M class File(models.Model): # 文档模型 name = models.CharField(max_length=255) staff = models.ForeignKey ...

  2. PHP回顾(4)文件相关函数

    touch()          创建文件 (修改时间,不存在时创建) copy()            复制文件,复制过程中可以修改文件名 rename()        重命名 或  移动文件  ...

  3. PHP编码相关函数试题

    1.检查字符串在指定的编码里是否有效的函数是什么? 2.获取字符编码的函数是什么? 3.解析 GET/POST/COOKIE 数据并设置全局变量的函数是什么? 4.大小写不敏感地查找字符串在另一个字符 ...

  4. day08-Python运维开发基础(文件操作与相关函数、函数基础)

    1. 文件操作及相关函数 # ### 文件操作 """ fp = open("文件名称",mode=模式,encoding=编码集) fp 文件io对 ...

  5. VC++ CArchive及简单的文件操作方法

    CArchive 方法用于存取文件 我向你推荐的是使用CArchive,它的使用方法简单且功能十分强大.首先还是用CFile声明一个对象,然后用这个对象的指针做参数声明一个CArchive对象,你就可 ...

  6. 【Linux C中文函数手册】文件内容控制函数

    文件内容控制函数 1)clearerr 清除文件流的错误旗标 相关函数 feof表头文件 #include<stdio.h>定义函数 void clearerr(FILE * stream ...

  7. VC的文件操作

    各种关于文件的操作在程序设计中是十分常见,如果能对其各种操作都了如指掌,就可以根据实际情况找到最佳的解决方案,从而在较短的时间内编写出高效的代码,因而熟练的掌握文件操作是十分重要的.本文将对Visua ...

  8. ubuntu Linux下C语言open函数打开或创建文件与read,write函数详细讲解

    open(打开文件) 相关函数 read,write,fcntl,close,link,stat,umask,unlink,fopen 表头文件 #include<sys/types.h> ...

  9. CFile、CStdioFile、FILE和其他文件操作(转+总结)

    CFile.CStdioFile.FILE和其他文件操作(转+总结) 2010-04-10 20:36:33|  分类: VC++|举报|字号 订阅     下载LOFTER我的照片书  |     ...

随机推荐

  1. 分布式版本控制系统Git-----8.fst-forward与no fast foward

    当前分支合并到另一分支时,如果没有分歧解决,就会直接移动文件指针.这个过程叫做fastforward. 举例来说,开发一直在master分支进行,但忽然有一个新的想法,于是新建了一个develop的分 ...

  2. zoj 3204 Connect them

    最小生成树,我用的是并查集+贪心的写法. #include<stdio.h> #include<string.h> #include<math.h> #includ ...

  3. thinkphp 的两种建构模式 第一种一个单入口里面定义两个模块,前台和后台,函数控制模块必须function.php前台加载前台模块的汉书配置文件,后台加载后台模块的汉书配置文件,公共文件共用。第二种架构模式两个单入口文件,分别生成两个应用定义define。。。函数可以定义配置文件。。。。

    thinkphp 的两种建构模式  第一种一个单入口里面定义两个模块,前台和后台,函数控制模块必须function.php前台加载前台模块的汉书配置文件,后台加载后台模块的汉书配置文件,公共文件共用. ...

  4. bzoj4318: OSU!&&CF235BLet's Play Osu!

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=4318 4318: OSU! Time Limit: 2 Sec  Memory Limit ...

  5. hdu_4417_Super Mario(主席树)

    题目链接:hdu_4417_Super Mario 题意: 给你n个树,有m个询问,每个询问有一个区间和一个k,问你这个区间内不大于k的数有多少个. 题解: 考虑用主席树的话就比较裸,当然也可以用其他 ...

  6. nginx在linux下的目录结构

    配置文件目录 putty 下  whereis nginx /etc/nginx

  7. 线索thread二叉树

    对于一个普通的二叉树 我们可以很明显的看到,在一个二叉树中,会有许多的空结点,而这些空结点必然会造成空间的浪费,为了解决这个问题,我们可以引入线索二叉树,把这些空结点利用起来,利用 '^' 记录给定结 ...

  8. marble 基本函数(一)

    . 标记 GeoDataPlacemark *place = new GeoDataPlacemark( "Bucharest" ); place->setCoordinat ...

  9. CevaEclipse - 常用设置

    1. 往工程里面添加在硬盘上已有的文件 File -> Import.. -> General -> File System From directory Browse... 勾选需 ...

  10. POJ 2083 Fractal 分形

    去年校赛团队赛就有一道分形让所有大一新生欲生欲死…… 当时就想学了 结果一直拖到…… 今天上午…… 马上要省选了 才会一点基础分形…… 还是自己不够努力啊…… 分形主要是要找到递归点…… 还有深度…… ...