资源描述:
《网页制作与网站建设.part.09》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、第9章PHP文件编程9.1文件操作9.2目录操作9.3上传文件19.1文件操作9.1.1检查文件是否存在函数file_exists()检查一个文件或目录是否存在:boolfile_exists(stringfilename)9.1.2打开和关闭文件1.打开文件用函数fopen()打开一个文件或URL:resourcefopen(stringfilename,stringmode[,booluse_include_path[,resourcezcontext]])2.关闭文件使用fclose()函数关闭文件:boolfclose(resourcehandle)9.1.3
2、写入文件用fwrite()函数向文件写入数据:intfwrite(resourcehandle,stringstr[,intlength])29.1文件操作9.1.4读取文件1.读取单个字符stringfgetc(resourcehandle)2.逐行读取数据fgets()函数从文件中读取一行:stringfgets(inthandle[,intlength])fgetss()函数从文件中读取一行并过滤掉HTML标记:stringfgetss(resourcehandle[,intlength[,stringallowable_tags]])fgetcsv()函数从文
3、件指针中读入一行并解析CSV字段:arrayfgetcsv(inthandle[,intlength[,stringdelimiter[,stringenclosure]]])39.1文件操作9.1.4读取文件3.读取指定长度数据用fread()函数可以从文件读取指定长度的数据:stringfread(inthandle,intlength)4.读取整个文件readfile()函数读取并输出一个文件的内容:intreadfile(stringfilename[,booluse_include_path[,resourcecontext]])fpassthru()函数输
4、出文件指针处所有的剩余数据:intfpassthru(resourcehandle)file()函数将整个文件读入一个数组中:arrayfile(stringfilename[,intuse_include_path[,resourcecontext]])file_get_contents()函数将整个文件读入一个字符串:stringfile_get_contents(stringfilename[,booluse_include_path[,resourcecontext[,intoffset[,intmaxlen]]]])49.1文件操作9.1.5文件定位fsee
5、k()函数在文件中定位文件指针:intfseek(resourcehandle,intoffset[,intwhence])rewind()函数将文件指针设置到文件开头:boolrewind(resourcehandle)ftell()函数返回文件指针读/写的位置:intftell(resourcehandle)feof()函数测试文件指针是否到了文件结束的位置:boolfeof(resourcehandle)59.1文件操作9.1.6检查文件属性fileatime()函数取得文件的上次访问时间:intfileatime(stringfilename)filectim
6、e()函数取得文件的创建时间:intfilectime(stringfilename)filemtime()函数取得文件的修改时间:intfilemtime(stringfilename)filesize()函数取得文件的大小:intfilesize(stringfilename)filetype()函数取得文件的类型:stringfiletype(stringfilename)69.1文件操作9.1.7重命名文件用rename()函数对一个文件或目录进行重命名:boolrename(stringoldname,stringnewname[,resourceconte
7、xt])9.1.8复制文件用copy()函数可以实现文件复制功能:boolcopy(stringsource,stringdest)9.1.9删除文件用unlink()函数可以删除指定的文件:boolunlink(stringfilename)79.2目录操作9.2.1创建目录用mkdir()函数可以创建一个目录:boolmkdir(stringpathname[,intmode])9.2.2获取和更改当前目录用getcwd()函数可以取得当前工作目录:stringgetcwd(void)用chdir()函数可以改变当前目录:boolchdir(st