一.问答题

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. String.IsNullOrEmpty()和String.IsNullOrWhiteSpace()的区别

    string.IsNullOrEmpty 这个是判断字符串是否为:null或者string.Empty或者“”,但不包含空格 .如果是如"\t"或者“   ” 这样的字符就返回fa ...

  2. 转:Android应用性能测试

    Android用户也许会经常碰到以下的问题: 1)应用后台开着,手机很快没电了——应用耗电大 2)首次/非首次启动应用,进入应用特别慢——应用启动慢 3)应用使用过程中,越来越卡——CPU能力不足/内 ...

  3. openresty 前端开发轻量级MVC框架封装二(渲染篇)

    这一章主要介绍怎么使用模板,进行后端渲染,主要用到了lua-resty-template这个库,直接下载下来,放到lualib里面就行了,推荐第三方库,已经框架都放到lualib目录里面,lua目录放 ...

  4. 第14天dbutils与案例

    第14天dbutils与案例 第14天dbutils与案例    1 1.    1.dbutils介绍    2 2.    2.dbutils快速入门    2 3.    3.dbutils A ...

  5. Save Update saveOrUpdate delete

    参考:Hibernate Session的saveOrUpdate()方法 saveOrUpdate与Update的作用 Hibernate Delete query Hibernate Basics ...

  6. spring注解支持

    Spring基于注解实现Bean定义支持如下三种注解: Spring自带的@Component注解及扩展@Repository.@Service.@Controller JSR-250 1.1版本中中 ...

  7. HDU 1907 John(取火柴博弈2)

    传送门 #include<iostream> #include<cstdio> #include<cstring> using namespace std; int ...

  8. hdu2141AC代码分享

    #include <iostream> #include <algorithm> using namespace std; const int N = 505; /////// ...

  9. MySQL聚簇索引

    聚簇索引并不是一种单独的索引类型,而是一种数据存储方式.具体的细节依赖于其实现方式,但innoddb 的聚簇索引实际上在同一个结构中保存了B-Tree索引和数据行. 当表有聚簇索引时,它的数据实际上存 ...

  10. HDU 1009 FatMouse' Trade(贪心)

    FatMouse' Trade Problem Description FatMouse prepared M pounds of cat food, ready to trade with the ...