向搜索引擎提交网站地图,软件外包公司主营业务,销售策略和营销策略,google推广 的效果1. 文件操作1.1 打开关闭文件fopen()resource fopen ( string filename, string mode [, bool use_include_path [, resource zcontext]] )?fopen()函数将resource绑定到一个流或句柄。绑定之后#xff0c;脚本就可以通过句柄与此资源交互;例1:以只读方式打开一个位于本地服务…1. 文件操作1.1 打开关闭文件fopen()resource fopen ( string filename, string mode [, bool use_include_path [, resource zcontext]] )?fopen()函数将resource绑定到一个流或句柄。绑定之后脚本就可以通过句柄与此资源交互;例1:以只读方式打开一个位于本地服务器的文本文件$fh fopen(test.txt, r);例2以只读方式打开一个远程文件$fh fopen(http://www.baidu.com, r);fclose()bool fclose ( resource handle )将 handle 指向的文件关闭 。如果成功则返回 TRUE失败则返回 FALSE;文件指针必须有效并且是通过 fopen() 或 fsockopen() 成功打开的;虽然每个请求最后都会自动关闭文件但明确的关闭打开的所有文件是一个好的习惯;例$fh fopen(test.txt, r);fclose($fh)1.2 读取文件php 提供了很多从文件中读取数据的方法不仅可以一次只读取一个字符还可以一次读取整个文件。fread()string fread ( int handle, int length )?fread()函数从handle指定的资源中读取length个字符,当到达EOF或读取到length个字符时读取将停止。如果要读取整个文件使用filesize()函数确定应该读取的字符数;例$file test.txt;$fh fopen( $file, r);$str fread($fh, filesize($file));echo $str;fclose($fh);fgets()string fgets ( int handle [, int length] )?fgets()函数从handle指定的资源中读取一行字符。碰到换行符(包括在返回值中)、EOF 或者已经读取了 length - 1 字节后停止(看先碰到那一种情况);例逐行读取文件$handle fopen(data.txt, r);while(!feof($handle)){$content fgets($handle);$content iconv(‘gbk‘,‘utf-8‘,$content);echo $content.”;}fclose($handle);注意如果没有指定 length则默认为 1K或者说 1024 字节。file()array file ( string $filename [, int $flags 0 [, resource $context ]])file()函数将文件读取到数组中各元素由换行符分隔。例$arr file(test.txt);print_r($arr);file_get_contents()string file_get_contents ( string filename [, bool use_include_path [, resource context [, int offset [, int maxlen]]]] )file_get_contents()函数将文件内容读到字符串中;例$str file_get_contents(test.txt);echo $str;1.3 写入文件fwrite()int fwrite ( resource handle, string string [, int length] )fwrite()函数将string的内容写入到由handle指定的资源中。如果指定length参数将在写入Length个字符时停止。例$str test text;$fh fopen(test.txt, a);fwrite($fh, $str);fclose($fh);file_put_contents()int file_put_contents ( string filename, string data [, int flags [, resource context]] )file_put_contents()函数将一个字符串写入文件与依次调用fopen(),fwrite(),fclose()功能一样;例$str hello;file_put_contents(test.txt, $str);1.4 复制重命名删除文件copy()bool copy ( string source, string dest )将文件从 source 拷贝到 dest。如果成功则返回 TRUE失败则返回 FALSE。例Copy(test.txt, test.txt.bak);rename()bool rename ( string oldname, string newname [, resource context] )尝试把 oldname 重命名为 newname。 如果成 功则返回 TRUE失败则返回 FALSE。例rename(test.txt, “test2.txt”);unlink()bool unlink ( string filename )删除文件如果删除成功返回true, 否则返回false;例1删除一个文本文件unlink(“test.txt)1.5 读取目录copy()bool copy ( string source, string dest )将文件从 source 拷贝到 dest。如果成功则返回 TRUE失败则返回 FALSE。例Copy(test.txt, test.txt.bak);rename()bool rename ( string oldname, string newname [, resource context] )尝试把 oldname 重命名为 newname。 如果成功则返回 TRUE失败则返回 FALSE。例rename(test.txt, “test2.txt”);unlink()bool unlink ( string filename )删除文件如果删除成功返回true, 否则返回false;例1删除一个文本文件unlink(“test.txt)scandir()array scandir ( string directory [, int sorting_order [, resource context]] )返回一个包含有 directory 中的文件和目录的数组;rmdir()bool rmdir ( string dirname )删除目录mkdir()bool mkdir ( string pathname [, int mode [, bool recursive [, resource context]]] )?尝试新建一个由 pathname 指定的目录。1.6 其他文件操作函数filesize()int filesize ( string filename )取得文件的大小以字节为单位filectime()int filectime ( string filename )取得文件的创建时间以unix时间戳方式返回例$t filectime(test.txt);echo date(Y-m-d H:i:s, $t);fileatime() 返回文件的最后改变时间;filemtime() 返回文件的最后修改时间;注”最后改变时间”不同于 “最后修改时间”。最后改变时间指的是对文件inode数据的任何改变包括改变权限所属组拥有者等; 而最后修改时间指的是对文件内容的修改file_exists() 检查文件或目录是否存在如果存在返回true, 否则返回false;is_readable() 判断文件是否可读如果文件存在并且可读则返回true;is_writable() 判断文件是否可写如果文件存在并且可写则返回true;1.7 解析目录路径函数basename()string basename ( string path [, string suffix] )返回路径中的文件名部份当指定了可选参数suffix会将这部分内容去掉;例2. 课上练习代码//打开文件$rh fopen(‘PHP_3.txt‘, ‘r‘);//读取文件第一个参数是文件句柄第二个是读取方式//计算文件大小(字节)$num filesize(‘PHP_3.txt‘);$str fread($rh, $num);echo $str;//如果设置文件访问错误需要去更改文件的权限属性 -- 右下角-- 开放权限 -- 改为可读可写echo ;//换行读取 识别 enter 不识别 $str_1 fgets($rh);$str_2 fgets($rh);//换行读取再次读取还会继续上次的读取位置继续读取echo $str_1;echo ;echo $str_2;//file 将文件内容转化为数组直接转化为换行回车作为分隔符$arr file(‘PHP_3.txt‘);print_r($arr);echo ;//file_get_contents 读取文件内容返回字符串并且可以读取外部网络数据// echo file_get_contents(‘PHP_3.txt‘);//直接读取网站,存到一个文本中可以直接获取对方的页面静态布局注意是静态的// $str_3 file_get_contents(‘http://www.lanou3g.com‘);// file_put_contents(‘PHP_3.txt‘, $str_3);//重命名// rename(‘PHP_3.txt‘, ‘1.txt‘);// rename(‘1.txt‘,‘PHP_3.txt‘);//文件拷贝 使用../ 替代上级文件夹// copy(‘PHP_3.txt‘, ‘../test.txt‘);//读取目录//1.打开文件目录句柄 .(一个点) 获取本级目录 ..(两个点)是上级目录$rh_1 opendir(‘.‘);// $arr readdir()//readdir 获取文件目录这个和 MySQL 一样必须使用循环取出while ($num readdir($rh_1)) {//读取出来的echo $num;echo ;}//读取目录print_r(scandir(‘.‘));//创建一个新的文件夹// mkdir(‘asdasd‘);//删除整个文件夹 删除目录必须保证目录内部没有其他文件// $is_bool rmdir(‘1‘);//删除// unlink(‘PHP_3.txt‘);//获取文件创建时间echo filectime(‘PHP_3.txt‘);echo ;//返回文件最后访问的时间echo fileatime(‘PHP_3.txt‘);echo ;//解析文件具体名称echo basename(‘PHP_3.txt‘,‘txt‘);echo ;//获取当前文件所在的目录的名称echo dirname(‘file/PHP_3.txt‘);echo ;//返回全程拓展名文件名print_r(pathinfo(PHP_3.txt));//修改文件目录权限echo ;fclose($rh);fclose($rh_1);?