一.目录操作
1. 创建目录
创建目录mkdir(目录名称) //返回值为布尔
一次性创建主目录及其子目录
判断是不是一个目录is_dir //返回true,false
判断目录或文件是否存在file_exists //返回值为布尔
返回路径中的目录部分 dirname
2. 打开目录,
opendir — 打开目录句柄
opendir(目录路径) //返回的是一个目录句柄.
readdir(目标句柄) //读取目录中的条目,
目录名为零的处理
//目录名中文的处理iconv(in_charset, out_charset, str)//说明://将字符串由in_charset字符集 转为 out_charset字符集//如:iconv(‘gbk’, ‘utf-8’, $str); //将$str由字符集gbk,转为utf-8; ?
3. 关闭目录
Closedir(目标句柄);//关闭目录,是为了释放内存资源.
有打开必须有关闭. Opendir,与closedir成对出现.
4. 目录重命名, 移动目录
rename(oldname, newname) //将oldname修改为newname; //返回值为布尔
5. 删除目录,
rmdir(目录名称) //返回值为布尔
二.文件操作
1. 打开文件
打开文件的方式fopen
fopen(文件路径,打开模式); //返回的文件句柄;
is_file 文件判断,返回布尔.
2. 关闭文件
fopen //返回的文件句柄
fclose(文件句柄); //释放内存资源
有打开就有关闭.
3. 写入文件
fwrite(文件句柄,字符串);
4. 读取文件内容:
1) 读取一个字符(字节)
fgetc(文件句柄)
对读取到0时的处理:
2) 读取一行字符
fgets
实例图片计时器
1 <?php ?2 // 图片计数器 3 ?4 // 第一步:对计数器文本文件进行初始化,也就是创建计数器文件,并将0写入 5 ????$filename = ‘./count.txt‘; 6 ????if (!file_exists($filename)) { 7 ????????// 以写入的方法打开,返回文件句柄 8 ????????$handle = fopen($filename,‘w‘); 9 ????????// 将0写入10 ????????fwrite($handle,0);11 ????????// 关闭句柄12 ????????fclose($handle);13 ????}14 // 第二步:按行 读取 计数器中的数字,同时将数字加1,将新数字写入到计数器文本中15 ????$handle = fopen($filename,‘r+‘);//以r+模式打开文件,由于在此模式下可读可写16 ????$line = fgets($handle); //将计数器中的数字读取,78817 ????$line++; //78918 ????// 将加1后的新数字写入计数器19 ????rewind($handle); //将指针复位20 ????fwrite($handle,$line);//将789写入计数器21 ????fclose($handle);22 23 // 第三步:按字符读取 将计数器中的数字,一个一个读取,同时将读取的数字和image目录中的图片名称进行匹配24 ????$handle = fopen($filename,‘r‘);25 ????// 循环一个一个的读取计数器中的数字26 ????while (false !== ($char = fgetc($handle))) {27 ????????echo "<img src=images/$char.jpg />";28 ????}29 ????fclose($handle);30 ??>
3) 读取指定大小的内容,
fread(文件句柄,长度); //说明,长度的单位是字节
feof(文件句柄) //文件的指针在文件的末尾
4) 不用打开和关闭
直接将文件读入字符串 file_get_contents
直接将字符串写入文件 file_put_contents
5. 拷贝文件
5) 删除一个目录中所有子目录及文件
Rmdir //只能删除空目录
Unlink//只能删除文件
删除整个文件夹(里面包含文件)的小例子:
1 ?<?php ?2 // 删除一个目录中所有子目录和文件 3 // rmdir ?只能删除文件目录 4 // unlink ?5 ?// 定义函数 6 ?7 // $filename = ‘./phpMyAdmin‘; 8 // $filename = ‘./phpMyAdmin‘; 9 /*function delAll($path){10 ????if (is_dir($path) ){11 ????????// 如果是目录则打开12 ????????$handle = opendir($path);13 ????????// 从目录句柄中循环读取14 ????????while (false !== ($file = readdir($handle))) {15 ????????????// 注意此时的.,..必须跳过,否则将删除你当前文件所在磁盘中的所有文件!!!!!16 ????????????if ($file==‘.‘||$file==‘..‘) {17 ????????????????continue;18 ????????????}19 ????????????// 如果是目录则递归,如果不是目录则直接unlink20 ????????????if (is_dir($path.‘/‘.$file) ){21 ????????????????delAll($path.‘/‘.$file);22 ????????????}else{23 ????????????????unlink($path.‘/‘.$file);24 ????????????}25 26 27 ????????}28 ????????closedir($handle);29 ????????rmdir($path);30 ????}31 }32 ?// 调用函数33 delAll($filename)34 ?>
还有几个经典练习例子:
?1 <!DOCTYPE html> ?2 <html lang="en"> ?3 <head> ?4 ????<meta charset="UTF-8"> ?5 ????<title>Document</title> ?6 </head> ?7 <body> ?8 ??9 ?10 <!-- ?11 第2题: 12 题型:代码题 13 难度:3级 14 题干: 15 编写一段程序,递归显示某个指定目录中(如phpMyAdmin)所有子目录及文件。 16 ?--> 17 <?php ?18 $filename = ‘./phpMyAdmin‘; 19 function showfiles($path){ 20 ????$handle = opendir($path); 21 ????echo "<ul>"; 22 ????while (false !== ($file = readdir($handle))){ 23 ????????if ($file =="."|| $file == "..") { 24 ????????????continue; 25 ????????} 26 ????????if (is_dir($path.‘/‘.$file)) { 27 ????????????showfiles($path.‘/‘.$file); 28 ????????}else { 29 ????????????echo "<li>$file</li>"; 30 ????????} 31 ????} 32 ????echo "</ul>"; 33 ????closedir($handle); 34 } 35 showfiles($filename); 36 ?37 ?38 ?39 ?40 ?41 // 第3题: 42 // 题型:代码题 43 // 难度:3级 44 // 题干: 45 // 编写一段程序,递归删除某个指定目录中(如phpMyAdmin)所有子目录及文件。 46 // 提示:在执行删除目录及文件时,一定要过虑掉“.”和“..”,避免删除上层目录中的目录及文件,十分危险。!!!!!!! 47 $filename = ‘./mytest‘; 48 function delfiles($path) { 49 ????$handle = opendir($path); 50 ????while (false !== ($file = readdir($handle))) { 51 ????????if ($file==‘.‘||$file==‘..‘) { 52 ????????????continue; 53 ????????} 54 ????????if (is_dir($path.‘/‘.$file)) { 55 ????????????delfiles($path.‘/‘.$file); 56 ????????}else { 57 ????????????// 删除文件 58 ????????????unlink($path.‘/‘.$file); 59 ????????} 60 ????} 61 ????closedir($handle); 62 ????// 删除文件夹 63 ????rmdir($path); 64 } 65 ?66 delfiles($filename); 67 ?68 ?69 ?70 // 第4题: 71 // 题型:代码题 72 // 难度:3级 73 // 题干: 74 // 编写一段程序,用图片来实现访问量效果。当每次进入该页面,访问量加1,访问量值保存到记事本中。读取记事本中访问的数值,并将访问量的每一个数,用对应的图片显示出来。页面最后显示“你是第n位访问者”。 75 // 提示:参考答案中提供了0到9的数字图片。; 76 ?77 ?78 $filename = ‘./count.txt‘; 79 if (!file_exists($filename)) { 80 ????$handle = fopen($filename,‘w‘); 81 ????fwrite($handle,0); 82 ????fclose($handle); 83 } 84 ?85 $handle = fopen($filename,‘r+‘); 86 $line = fgets($handle); 87 $line++; 88 rewind($handle); 89 fwrite($handle,$line); 90 fclose($handle); 91 ?92 $handle = fopen($filename,‘r‘); 93 echo "<span style=‘color:blue;font-size:30px;font-weight:700‘>你是第</span>"; 94 while (false !== ($file=fgetc($handle))) { 95 ????echo "<img src=images/$file.jpg />"; 96 } 97 echo "<span style=‘color:blue;font-size:30px;font-weight:700‘>位访问者!</span>"; 98 fclose($handle); 99 100 101 /*102 第一题!!!递归应用103 复制文件:并且从一个文件夹移动到另一个文件夹*/104 105 $filename = ‘./phpMyAdmin‘;106 // $aimfilename =‘./phpMyAdmin_copy‘;107 ?// $aimfilename =‘../student/a/b/c/phpMyAdmin_copy‘;108 function read_copy($dirpath,$aim_path) {109 ????$handle_dir = opendir($dirpath);110 ????while (false !== ($file = readdir($handle_dir))) {111 ????????if ($file==‘.‘||$file==‘..‘) {112 ????????????continue;113 ????????}114 ????????if (is_dir($dirpath.‘/‘.$file)) { //如果是文件夹115 ????????????if (file_exists($aim_path.‘/‘.$file)) { ?//有该文件夹的话116 ????????????????read_copy($dirpath.‘/‘.$file, $aim_path.‘/‘.$file);117 ????????????} else { ?//没有该文件夹的话118 ????????????????mkdir($aim_path.‘/‘.$file,0777,true);119 ????????????????read_copy($dirpath.‘/‘.$file, $aim_path.‘/‘.$file);120 ????????????}121 ????????????122 ????????} else { ?//如果是文件123 ????????????if (file_exists($aim_path)) {124 ????????????????copy($dirpath.‘/‘.$file, $aim_path.‘/‘.$file);125 ????????????} else {126 ????????????????mkdir($aim_path,0777,true);127 ????????????????copy($dirpath.‘/‘.$file,$aim_path.‘/‘.$file);128 ????????????}129 ????????????130 ????????}131 ????????132 ????}133 ????closedir($handle_dir);134 }135 read_copy($filename,$aimfilename);136 ?>137 138 139 ????140 </body>141 </html>
PHP核心编程--目录操作(包含文件操作)
原文地址:http://www.cnblogs.com/mrszhou/p/7499910.html