php 文件函数

文件操作

1. 检查文件是否存在

bool file_exists( string filename);

2. 打开和关闭文件

1resource fopen( string filename , string mode);

2bool fclose(resource handle);

3. 读取文件

1读取单个字符 string fgetc ( resource handle);

2逐行读取

1  string fgets( resource handle [,int length ]);

         string fgetss()  

l  array fgetcsv( resource handle [, int length [, string delimiter [, string enclosuer]]]) ;

3 读取指定长度

         string fread( int handle , int length);

4 读取整个文件

1 int readfile(string filename);

2 fpassthru( resource handle) 输出文件指针处所有的剩余数据。

3 array file( string filename) 将整个文件读入一个数组。

4 string file_get_contents( string filename) 将整个文件读入一个字符串。

4. 文件定位

1 . bool rewind( resource handle);

2 . feof() 测试文件指针是否到了文件结束的位置。

5. 写入文件

int fwrite( resource handle , string str [,int length]);

file_put_contents(string filename,string contents,flags);

6. 检查文件属性

1. int fileatime( stirng filename) ;  2.  int filectime(string filename)

3. int filemtime(string filename); 4 int filesize(string filename);

7. 重命名文件

bool rename( string oldname , string newname [, resource context]);

8. 复制文件

bool copy( string source ,string dest);

9. 删除文件

bool unlink(string filename);

目录操作

1. 创建目录

         bool mkdir( string pathname);

2. 打开和关闭目录

         resource opendir( string path )

         void closedir ( resource dir_handle);

3. 获取和更改当前目录

         string getcwd(void);

         bool chdir ( string directory);

4. 读取目录

         string readdir( resource dir_handle);

此函数返回目录中下一个文件的文件名。文件名按照在文件系统中的排列顺序返回。如果成功则返回文件名,如果失败返回false.

5. 浏览目录

         array  scandir( string  directory [,int sorting_order])

如果成功则函数返回一个数组,该数组包含有 directory 中的文件和目录: 如果失败则返回 false. 如果 directory 不是一个目录,则返回布尔值 false 并生成一个 E_WARNING 错误。

6. 删除目录

bool rmdir( string dirname);

此函数尝试删除参数 dirname 所指定的目录。该目录必须是空的,而且要有相应的权限。如果成功则返回 true ,失败则返回 false。

7 解析路径

array pathinfo( sting path);

此函数返回一个联合数组包含有该路径的信息。该数组包括以下的数组元素:

dirname(目录名)  bassename(基本文件名) extension(文件扩展名)

8检查磁盘空间

float disk_total_space( string directory);

float disk_free_space( string directory);

文件上传

MAX_FILE_SIZE 隐藏字段必须放在文件域之前,其值为接收文件的最大尺寸。这是对浏览器的一个建议,PHP也会检查此项。建议在表单中在文件域之前加上此项。

全局变量 $_FILES

$_FILES[“userfile”][“name”]客户端机器文件的原名称

$_FILES[“userfile”][“type”]: 文件的MIME类型

$_FILES[“userfile”][“size”]: 已上传文件的大小,单位为字节

$_FILES[“userfile”][“tmp_name”]: 文件被上传后在服务端存储的临时文件名

$_FILES[“userfile”][“error”]: 文件上传相关的错误代码

 

两个函数

1. 将上传的文件移动到新位置

bool move_uploaded_file (string filename , string destination);

如果移动文件成功,则返回 true 。如果filename 不是合法上传的文件,则不会出现任何操作, move_uploaded_file() 将返回 false。

2. bool is_uploaded_file( string filename);

         如果 filename 所给出的文件是通过 HTTP POST 上传的,则返回 true。这可以用来确保恶意的用户无法欺骗脚本去访问原本不能访问的文件。

为了能使 is_uploaded_file() 函数正常工作,应指定类似与 $_FILES[“username’][‘tmp_name’]的变量,而在从客户端上传的文件名$_FILES[“username’][‘name’]不能正常工作。

原文地址:https://www.cnblogs.com/air5215/p/5351794.html