建设一个网站需要做哪些工作内容,个人网站建设价格表,asp网站如何安装,app开发工具简单前面的话 在程序运行时#xff0c;程序本身和数据一般都存在内存中#xff0c;当程序运行结束后#xff0c;存放在内存中的数据被释放。如果需要长期保存程序运行所需的原始数据#xff0c;或程序运行产生的结果#xff0c;就需要把数据存储在文件或数据库。一般地#x…前面的话 在程序运行时程序本身和数据一般都存在内存中当程序运行结束后存放在内存中的数据被释放。如果需要长期保存程序运行所需的原始数据或程序运行产生的结果就需要把数据存储在文件或数据库。一般地小型数据存储在文件中海量数据存储在数据库中。本文主要介绍php中目录和文件的基本操作 文件类型 文件一般指存储在外部介质上具有名字文件名的一组相关数据集合。用文件可长期保存数据并实现数据共享 PHP是以UNIX的文件系统为模型的。因此在Windows系统中我们只能获得”file”、”dir”或者“unknown”三种文件类型。而在UNIX系统中我们可以获得block、char、dir、fifo、file、link和unknown七种类型 可以使用函数filetype()获取文件的具体类型可能的值有fifochardirblocklinkfile 和 unknown string filetype ( string filename ) 如果出错则返回 FALSE。如果调用失败或者文件类型未知的话 filetype() 还会产生一个 E_NOTICE 消息 在服务器中新建一个目录test并在目录中新建一个文件a.txt ?php
echo filetype(test/a.txt); // file
echo filetype(test/); // dir
echo filetype(test/b.txt); // Warning: filetype(): Lstat failed for test/b.txt
? 在这7种文件类型中window系统常用的是file和dir这两种它们配套的类型检测函数分别是is_dir( )和is_file( ) is_dir( ) 判断给定文件名是否是一个目录。如果文件名存在并且是一个目录则返回 true否则返回 false bool is_dir(_name) is_file( ) 判断给定文件名是否为一个正常的文件如果文件存在且为正常的文件则返回 true bool is_file(_name) ?php
var_dump (is_file(test/a.txt)); //boolean true
var_dump (is_dir(test/)); //boolean true
? 文件属性 一般地在文件或目录右键菜单中选择属性即可查看文件的属性 下表中列出了php中关于文件属性的常用函数 ?php
var_dump (file_exists(test/a.txt)); //boolean true
var_dump (filesize(test/a.txt)); // int 0
var_dump (is_readable(test/a.txt)); //boolean true
var_dump (is_writeable(test/a.txt)); //boolean true
var_dump (is_executable(test/a.txt)); //boolean false
var_dump (date(Y-m-d H:i:s,(filectime(test/a.txt))));//string 2016-11-22 06:47:54 (length19)
var_dump (date(Y-m-d H:i:s,(filemtime(test/a.txt))));//string 2016-11-22 06:47:54 (length19)
var_dump (date(Y-m-d H:i:s,(fileatime(test/a.txt))));//string 2016-11-22 06:47:54 (length19)
? 目录路径 windows下的目录路径使用是正斜杠(\)而unix下的目录路径使用是反斜杠(/) $unixPath/var/www/html/index.php; //在UNIX系统中的绝对路径必须使用/分隔
$winPathC:\\Appserv\\www\\index.php; //在Windows系统的绝对路径默认使用\分隔
$winPath2C:/Appserv/www/index.php; //在Windows系统中也可使用“/”分隔 因为在Windows系统中也可使用(/)分隔。所以在PHP中不论是什么操作系统全部都使用反斜杠(/)代表路径分隔符号 在PHP中还提供了一个常量DIRECTORY_SEPARATOR以此来代表目录分隔符但写起来较麻烦 ?php
echo c:.DIRECTORY_SEPARATOR.a.DIRECTORY_SEPARATOR.b.DIRECTORY_SEPARATOR.c; //c:\a\b\c
? 在windows下多个路径的分隔符使用分号(;)分隔而unix下使用冒号(:)分隔 在PHP中提供了一个常量PATH_SEPARATOR用来在跨平台的情况下表示多个路径之间的分隔符 ?php
echo aaa/ccc/ddd.PATH_SEPARATOR./www/yyyy;//aaa/ccc/ddd;/www/yyyy
? 换行 在window下换行是\r\n而在unix下换行是\n。通常在写程序中换行就以unix为准写作\n 同样地PHP提供了一个常量PHP_EOL用来在跨平台的情况下表示换行 .和.. 在PHP中.表示当前目录..表示上一级目录 ?php
var_dump (file_exists(test/a.txt));//boolean true
var_dump (file_exists(./test/a.txt));//boolean true
var_dump (file_exists(../www/test/a.txt));//boolean true
? 根路径 有两种根路径需要进行区分一种是客户端根路径一种是服务器根路径 以我自己在d盘安装的wamp为例客户端根路径指d:\wamp\www\而服务器根路径为为d:\ ?php
echo img src/a.jpg;//客户端根路径相当于d:\wamp\www\a.jpg
mkdir(/hello);//服务器根路径相当于d:\hello
? 路径解析函数 【basename()】 basename()函数用于返回路径中的文件名部分 ?php
echo 1) .basename(/etc/sudoers.d, .d);//1) sudoers
echo 2) .basename(/etc/passwd).PHP_EOL;//2) passwd
echo 3) .basename(/etc/).PHP_EOL;//3) etc
echo 4) .basename(.).PHP_EOL;//4) .
echo 5) .basename(/);//5)
? 【dirname()】 dirname()函数用于返回路径中的目录部分 ?php
echo 1) . dirname(/etc/passwd) . PHP_EOL; // 1) /etc
echo 2) . dirname(/etc/) . PHP_EOL; // 2) \
echo 3) . dirname(.); // 3) .
? 【pathinfo()】 pathinfo()函数用于返回文件路径的信息 ?php
$path_parts pathinfo(/www/htdocs/inc/lib.inc.php);
echo $path_parts[dirname], \n;// /www/htdocs/inc 目录名
echo $path_parts[basename], \n;// lib.inc.php 文件名
echo $path_parts[extension], \n;// php 文件后缀
echo $path_parts[filename], \n; // lib.inc 文件名不带后缀
? 【realpath()】 realpath()函数用于返回规范化的绝对路径名 在Windows上realpath()会将unix风格的路径改成Windows风格的 ?php
echo realpath(/wamp);// D:\wamp
? 目录遍历 glob() glob()函数用于寻找与模式匹配的文件路径 array glob ( string $pattern [, int $flags 0 ] ) 在www目录下新建a.txt和b.txt文件 ?php
foreach (glob(*.txt) as $filename) {//a.txt size 1050 b.txt size 73echo $filename size . filesize($filename) . \n;
}
? opendir() opendir()函数用于打开目录句柄。如果成功则返回目录句柄的resource失败则返回 FALSE resource opendir ( string $path [, resource $context ] ) ?php
var_dump(opendir(test))//resource(3, stream)
? closedir() closedir()函数用于关闭目录句柄 void closedir ([ resource $dir_handle ] ) 参数dir_handle表示目录句柄的 resource之前由 opendir()所打开的。如果目录句柄没有指定那么会假定为是opendir()所打开的最后一个句柄 ?php
$dir opendir(test);
closedir($dir);
? readdir() readdir()函数用于从目录句柄中读取条目返回目录中下一个文件的文件名。文件名以在文件系统中的排序返回失败时返回 FALSE string readdir ([ resource $dir_handle ] ) 在www目录下新建目录test并在目录test下新建a.txt和b.txt文件 ?php
$dir opendir(test);
echo readdir($dir).br;//.
echo readdir($dir).br;//..
echo readdir($dir).br;//a.txt
echo readdir($dir).br;//b.txt
echo readdir($dir).br;//
closedir($dir);
? 在遍历目录时每个目录的前两个返回值都是.和...代表当前目录..代表上一级目录 所以一般地列出当前目录的所有文件并去掉 . 和 ..常采用下面的代码 ?php
if ($handle opendir(test)) {while (false ! ($file readdir($handle))) {if ($file ! . $file ! ..) {echo $file\n;}}closedir($handle);
}
? 接下来在test目录下新建一个目录in并在in目录中新建文件c.txt。然后目录和文件区分显示 [注意]通过is_dir()函数判断目录时需要加入路径 ?php
if ($handle opendir(test)) {while (false ! ($file readdir($handle))) {if ($file ! . $file ! ..) {$file test/.$file;if(is_dir($file)){echo 目录.$file.br;}else{echo 文件.$file.br;}}}closedir($handle);
}
/*
文件test/a.txt
文件test/b.txt
目录test/in*/
? rewinddir() rewinddir()函数用于倒回目录句柄将参数dir_handle指定的目录流重置到目录的开头 void rewinddir ( resource $dir_handle ) 如果不使用rewinddir()函数则文件只能遍历一次 ?php
if ($handle opendir(test)) {while (false ! ($file readdir($handle))) {if ($file ! . $file ! ..) {$file test/.$file;if(is_dir($file)){echo 目录.$file.br;}else{echo 文件.$file.br;}}}while (false ! ($file readdir($handle))) {if ($file ! . $file ! ..) {$file test/.$file;if(is_dir($file)){echo 目录.$file.br;}else{echo 文件.$file.br;}}}closedir($handle);
}/*
文件test/a.txt
文件test/b.txt
目录test/in*/
? 使用rewinddir()函数可以把目录句柄返回到第一个文件从而实现重新遍历 ?php
if ($handle opendir(test)) {while (false ! ($file readdir($handle))) {if ($file ! . $file ! ..) {$file test/.$file;if(is_dir($file)){echo 目录.$file.br;}else{echo 文件.$file.br;}}}rewinddir($handle);while (false ! ($file readdir($handle))) {if ($file ! . $file ! ..) {$file test/.$file;if(is_dir($file)){echo 目录.$file.br;}else{echo 文件.$file.br;}}}closedir($handle);
}/*
文件test/a.txt
文件test/b.txt
目录test/in
文件test/a.txt
文件test/b.txt
目录test/in*/
? 目录统计 disk_total_space() disk_total_space()函数返回一个目录的磁盘总大小 float disk_total_space ( string $directory ) ?php
$ds disk_total_space(C:);
echo $ds.br;//126652637184
$ds disk_total_space(D:);
echo $ds;//1000202240000
? disk_free_space() disk_free_space()函数返回目录中的可用空间 float disk_free_space ( string $directory ) ?php
$ds disk_free_space(C:);
echo $ds.br;//86087041024
$ds disk_free_space(D:);
echo $ds;//481647472640
? 下面来统计在www文件夹下新建的test目录的个数 ?php$dirn 0; //目录数$filen 0; //文件数//统计一个目录下的文件和目录的个数function getdirnum($file) {global $dirn;global $filen; $dir opendir($file);while (false ! ($filename readdir($dir))) {if($filename!. $filename !..) {$filename $file./.$filename; //更新路径if(is_dir($filename)) {$dirn;getdirnum($filename); //递归就可以查看所有子目录} else {$filen; }}}closedir($dir);}getdirnum(test);echo 目录数为:{$dirn}br;//目录数为:1echo 文件数为:{$filen}br;//文件数为:3
? 下面来统计在www文件夹下新建的test目录的大小 ?php//统计目录大小function dirsize($file) {$size 0;$dir opendir($file);while(false ! ($filename readdir($dir))) {if($filename!. $filename !..) {$filename $file./.$filename;if(is_dir($filename)) {$size dirsize($filename);//使用递归} else {$size filesize($filename);}}}closedir($dir);return $size;}
echo test目录大小为.dirsize(test).br;//test目录大小为302
? 目录增删 mkdir() mkdir()函数用于新建目录 bool mkdir ( string $pathname [, int $mode 0777 [, bool $recursive false [, resource $context ]]] ) rmdir() rmdir()函数用于删除目录 bool rmdir ( string $dirname [, resource $context ] ) [注意]该目录必须是空的而且要有相应的权限。失败时会产生一个 E_WARNING 级别的错误 unlink() unlink()函数用于删除文件 bool unlink ( string $filename [, resource $context ] ) 下面来清空test目录 ?phpfunction deldir($dirname) {//如果是文件直接删除即可if(is_file($dirname)) {unlink($dirname);}$dir opendir($dirname);while(FALSE ! ($filename readdir($dir))) {if($filename !. $filename!..) {$filename $dirname./.$filename;if(is_dir($filename)) {deldir($filename);//递归}else {unlink($filename);//删除文件}}}closedir($dir);if($dirname ! test){rmdir($dirname);//删除目录}}deldir(test);
? 目录复制 copy() copy()函数用于拷贝文件 bool copy ( string $source , string $dest [, resource $context ] ) [注意]copy()函数不能用于复制目录 ?php
$file a.txt;
$newfile a.bak;
copy($file, $newfile);
? rename() rename()函数用于重命名一个文件或目录 bool rename ( string $oldname , string $newname [, resource $context ] ) [注意]rename()函数具有移动文件或目录的功能 下面把www目录下的test目录剪贴命名为t并移动到d盘目录下 ?php
rename(test, d:/t);
? 使用rename()只能实现剪切的操作使用copy()只能复制文件。如果要复制目录则需要使用循环和遍历 ?php/*** $dirsrc 原目录* $dirto 目标目录*/function copydir($dirsrc, $dirto) {//如果目录不存在则新建一个目录if(!file_exists($dirto)) {mkdir($dirto);}$dir opendir($dirsrc);while(FALSE ! ($filename readdir($dir))) {if($filename ! . $filename !..) {$srcfile $dirsrc./.$filename; //原文件$tofile $dirto./.$filename; //目标文件if(is_dir($srcfile)) {copydir($srcfile, $tofile); //递归处理所有子目录}else{copy($srcfile, $tofile);//复制文件}}}}copydir(test, d:/t);
? 文件操作 touch() touch()函数用来设定文件的访问和修改时间。如果文件不存在则会被创建。成功时返回 TRUE 或者在失败时返回 FALSE bool touch ( string $filename [, int $time time() [, int $atime ]] ) 参数filename表示要设定的文件名time表示要设定的时间。如果没有提供参数 time 则会使用当前系统的时间atime表示如果给出了这个参数则给定文件的访问时间会被设为atime否则会设置为time。如果没有给出这两个参数则使用当前系统时间 ?phptouch(abc.txt)
? copy() copy()函数用于拷贝文件 bool copy ( string $source , string $dest [, resource $context ] ) [注意]copy()函数不能用于复制目录 ?php
$file a.txt;
$newfile a.bak;
copy($file, $newfile);
? rename() rename()函数用于重命名一个文件或目录 bool rename ( string $oldname , string $newname [, resource $context ] ) [注意]rename()函数具有移动文件或目录的功能 ?php
rename(abc.txt, d:/cba.txt);
? unlink() unlink()函数用于删除文件 bool unlink ( string $filename [, resource $context ] ) ?php
unlink(d:/cba.txt);
? 文件内容 fopen() fopen()函数用于打开文件或者URLfopen()将 filename 指定的名字资源绑定到一个流上 fopen() 中 mode 的可能值列表 mode 说明
r 只读方式打开将文件指针指向文件头。
r 读写方式打开将文件指针指向文件头。
w 写入方式打开将文件指针指向文件头并将文件大小截为零。如果文件不存在则尝试创建之。
w 读写方式打开将文件指针指向文件头并将文件大小截为零。如果文件不存在则尝试创建之。
a 写入方式打开将文件指针指向文件末尾。如果文件不存在则尝试创建之。
a 读写方式打开将文件指针指向文件末尾。如果文件不存在则尝试创建之。 ?php//使用绝对路径打开file.txt文件选择只读模式并返回资源$handle$handle fopen(/home/rasmus/file.txt, r);//访问文档根目录下的文件也以只读模式打开$handle fopen(“{$_SERVER[DOCUMENT_ROOT]}/data/info.txt, r);//在 Windows 平台上转义文件路径中的每个反斜线或者用斜线以二进制和只写模式组合$handle fopen(c:\\data\\file.gif, wb);//使用相对路径打开file.txt文件选择只读模式并返回资源$handle$handle fopen(../data/info.txt, r);//打开远程文件 使用HTTP协议只能以只读的模式打开$handle fopen(http://www.example.com/, r);//使用FTP协议打开远程文件如果FTP服务器可写则可以以写的模式打开$handle fopen(ftp://user:passwordexample.com/somefile.txt, w);
? fclose() fclose()函数用于关闭一个已打开的文件指针 bool fclose ( resource $handle ) ?php
$handle fopen(test/a.txt, r);
fclose($handle);
? fwrite() fwrite()函数用于写入文件可安全用于二进制文件返回写入的字符数出现错误时则返回 FALSE int fwrite ( resource $handle , string $string [, int $length ] ) 当打开方式为只读模式时无法向文件写入字符 ?php
$fp fopen(test/a.txt, r);
echo fwrite($fp, 1);//0
echo br;
echo fwrite($fp, 23);//0
echo br;
fclose($fp);
? 当打开方式为写模式时可以向文件写入字符 ?php
$fp fopen(test/a.txt, w);
echo fwrite($fp, 1);//1
echo br;
echo fwrite($fp, 23);//2
echo br;
fclose($fp);
/*
文件内容为123*/
? 当打开方式为追加模式时将向文件的尾部追加新的字符 ?php
$fp fopen(test/a.txt, a);
echo fwrite($fp, 1);//1
echo br;
echo fwrite($fp, 23);//2
echo br;
fclose($fp);
/*
刷新两次时文件内容为123123*/
? fgetc() fgetc()函数用于从文件指针中读取字符 [注意]使用fgetc()函数时需要在fopen()函数中使用读模式 string fgetc ( resource $handle ) ?php
$fp fopen(test/a.txt, r);
echo fgetc($fp);//1
echo fgetc($fp);//2
echo fgetc($fp);//3
fclose($fp);
? feof() feof()函数用于测试文件指针是否到了文件结束的位置 bool feof ( resource $handle ) ?php
$fp fopen(test/a.txt, r);
while(!feof($fp)){echo fgetc($fp);//123123
}
fclose($fp);
? fgets() fgets()函数用于从文件指针中读取一行 string fgets ( resource $handle [, int $length ] ) 将test目录下的a.txt文件内容修改为 aa
bbb ?php
$fp fopen(test/a.txt, r);
echo fgets($fp);//aa
echo fgets($fp);//bbb
echo fgets($fp);//
fclose($fp);
? fread() fread()函数用于读取文件可安全用于二进制文件。fread()从文件指针handle读取最多length个字节。该函数在读取了length个字节或到达了文件末尾(EOF)时将停止读取文件 string fread ( resource $handle , int $length ) ?php
$fp fopen(test/a.txt, r);
echo fread($fp,3);//aa
fclose($fp);$fp fopen(test/a.txt, r);
echo fread($fp,filesize(test/a.txt));//aa bbb
fclose($fp);
? fseek() fseek()函数用于在文件指针中定位成功则返回 0否则返回 -1 int fseek ( resource $handle , int $offset [, int $whence SEEK_SET ] ) 将test目录下的a.txt文件内容修改为12345 ?php
$fp fopen(test/a.txt, r);
echo fgetc($fp);//1
fseek($fp,4);
echo fgetc($fp);//5
fclose($fp);
? ?php
$fp fopen(test/a.txt, r);
echo fread($fp,2).br;//12
fseek($fp,4);
echo fread($fp,2).br;//5
fseek($fp,-3,SEEK_END);
echo fread($fp,2).br;//34
fclose($fp);
? ftell() ftell()函数用于返回文件指针读/写的位置 int ftell ( resource $handle ) ?php
$fp fopen(test/a.txt, r);
echo ftell($fp);//0
fgetc($fp);
echo ftell($fp);//1
fseek($fp,4);
echo ftell($fp);//4
fclose($fp);
? rewind() rewind()函数用于倒回文件指针的位置将handle的文件位置指针设为文件流的开头 bool rewind ( resource $handle ) ?php
$fp fopen(test/a.txt, r);
fseek($fp,2);
echo ftell($fp);//2
rewind($fp);
echo ftell($fp);//0
? file_get_contents() file_get_contents()函数用于将整个文件读入一个字符串 string file_get_contents ( string $filename [, bool $use_include_path false [, resource $context [, int $offset -1 [, int $maxlen ]]]] ) ?php
$homepage file_get_contents(test/a.txt);
echo $homepage;//12345
? 页面变为百度首页 ?php
$homepage file_get_contents(http://www.baidu.com/);
echo $homepage;
? file_put_contents() file_put_contents()函数用于将一个字符串写入文件 int file_put_contents ( string $filename , mixed $data [, int $flags 0 [, resource $context ]] ) 使用该函数和依次调用 fopen()fwrite() 以及 fclose() 功能一样 [注意]默认为写模式若设置第三个参数为FILE_APPEND则变为追加模式 ?php
file_put_contents(test/a.txt,abc);
? readfile() readfile()函数用于读取文件并写入到输出缓冲 int readfile ( string $filename [, bool $use_include_path false [, resource $context ]] ) ?php
readfile(http://www.baidu.com/);//页面中显示百度首页
?
?php
readfile(test/a.txt);//页面中显示abc
? file() file()函数用于把整个文件读入一个数组中每一行作为一个数组的元素 array file ( string $filename [, int $flags 0 [, resource $context ]] ) 将a.txt的文件内容改为每一行一个数字分别是1、2、3、4、5、6、7、8、9 ?php
$arr file(test/a.txt,0);
echo $arr[0].br;//1
echo count($arr);//9
? ftruncate() ftruncate()函数用于将文件截断到给定的长度 bool ftruncate ( resource $handle , int $size ) [注意]使用ftruncate()函数时需要使用追加模式。经测试使用读模式时无效使用写模式时文件内容被清空 ?php
$fp fopen(test/a.txt,a);
ftruncate($fp,100);
? 【转发自http://www.cnblogs.com/xiaohuochai/p/6088999.html】转载于:https://www.cnblogs.com/wenJiaQi/p/6192810.html