ThinkChat🤖让你学习和工作更高效,注册即送10W Token,即刻开启你的AI之旅 广告
[[官方文档-文件系统相关扩展]](http://php.net/manual/zh/refs.fileprocess.file.php) [TOC] ## [Direct IO](http://php.net/manual/zh/book.dio.php "Direct IO") ### Direct IO 函数 This extension is only available on Windows Platforms as of PHP 5.0.0. 此扩展已被移至 [» PECL](http://pecl.php.net/) 资源库且不再与 PHP 捆绑。5.1.0. 1. dio_close — Closes the file descriptor given by fd 2. dio_fcntl — Performs a c library fcntl on fd 3. dio_open — Opens a file (creating it if necessary) at a lower level than the C library input/ouput stream functions allow 4. dio_read — Reads bytes from a file descriptor 5. dio_seek — Seeks to pos on fd from whence 6. dio_stat — Gets stat information about the file descriptor fd 7. dio_tcsetattr — Sets terminal attributes and baud rate for a serial port 8. dio_truncate — Truncates file descriptor fd to offset bytes 9. dio_write — Writes data to fd with optional truncation at length ## [目录](http://php.net/manual/zh/book.dir.php "目录") ### Directory 类 1. Directory::close — 释放目录句柄 2. Directory::read — 从目录句柄中读取条目 3. Directory::rewind — 倒回目录句柄 ### 目录函数(9个) 1. chdir — 改变目录 2. chroot — 改变根目录 3. closedir — 关闭目录句柄 4. dir — 返回一个 Directory 类实例 5. getcwd — 取得当前工作目录 6. opendir — 打开目录句柄 7. readdir — 从目录句柄中读取条目 8. rewinddir — 倒回目录句柄 9. scandir — 列出指定路径中的文件和目录 ## [Fileinfo](http://php.net/manual/zh/book.fileinfo.php "Fileinfo") 本模块中的函数通过在文件的给定位置查找特定的 *魔术* 字节序列 来猜测文件的内容类型以及编码。 虽然不是百分百的精确, 但是通常情况下能够很好的工作。 ### finfo 类 1. finfo::buffer — 别名 finfo_buffer() 2. finfo::__construct — 别名 finfo_open 3. finfo::file — 别名 finfo_file() 4. finfo::set_flags — 别名 finfo_set_flags() ### fnfo 函数(6) 1. finfo_buffer — 返回一个字符串缓冲区的信息 2. finfo_close — 关闭 fileinfo 资源 3. finfo_file — 返回一个文件的信息 4. finfo_open — 创建一个 fileinfo 资源 5. finfo_set_flags — 设置 libmagic 配置选项 6. mime_content_type — 检测文件的 MIME 类型 ## [文件系统](http://php.net/manual/zh/book.filesystem.php "文件系统") 编译此扩展无需任何额外的库,但想要 PHP 支持 Linux 的 LFS(large files),需要最新的 glibc,并在编译 PHP 时带上以下标记: *\-D\_LARGEFILE\_SOURCE -D\_FILE\_OFFSET\_BITS=64*。 ### 文件系统函数(80个) 1. basename — 返回路径中的文件名部分 2. chgrp — 改变文件所属的组 3. chmod — 改变文件模式 4. chown — 改变文件的所有者 5. clearstatcache — 清除文件状态缓存 6. copy — 拷贝文件 7. delete — 参见 unlink 或 unset 8. dirname — 返回路径中的目录部分 9. disk_free_space — 返回目录中的可用空间 10. disk_total_space — 返回一个目录的磁盘总大小 11. diskfreespace — disk_free_space 的别名 12. fclose — 关闭一个已打开的文件指针 13. feof — 测试文件指针是否到了文件结束的位置 14. fflush — 将缓冲内容输出到文件 15. fgetc — 从文件指针中读取字符 16. fgetcsv — 从文件指针中读入一行并解析 CSV 字段 17. fgets — 从文件指针中读取一行 18. fgetss — 从文件指针中读取一行并过滤掉 HTML 标记 19. file_exists — 检查文件或目录是否存在 20. file_get_contents — 将整个文件读入一个字符串 21. file_put_contents — 将一个字符串写入文件 22. file — 把整个文件读入一个数组中 23. fileatime — 取得文件的上次访问时间 24. filectime — 取得文件的 inode 修改时间 25. filegroup — 取得文件的组 26. fileinode — 取得文件的 inode 27. filemtime — 取得文件修改时间 28. fileowner — 取得文件的所有者 29. fileperms — 取得文件的权限 30. filesize — 取得文件大小 31. filetype — 取得文件类型 32. flock — 轻便的咨询文件锁定 33. fnmatch — 用模式匹配文件名 34. fopen — 打开文件或者 URL 35. fpassthru — 输出文件指针处的所有剩余数据 36. fputcsv — 将行格式化为 CSV 并写入文件指针 37. fputs — fwrite 的别名 38. fread — 读取文件(可安全用于二进制文件) 39. fscanf — 从文件中格式化输入 40. fseek — 在文件指针中定位 41. fstat — 通过已打开的文件指针取得文件信息 42. ftell — 返回文件指针读/写的位置 43. ftruncate — 将文件截断到给定的长度 44. fwrite — 写入文件(可安全用于二进制文件) 45. glob — 寻找与模式匹配的文件路径 46. is_dir — 判断给定文件名是否是一个目录 47. is_executable — 判断给定文件名是否可执行 48. is_file — 判断给定文件名是否为一个正常的文件 49. is_link — 判断给定文件名是否为一个符号连接 50. is_readable — 判断给定文件名是否可读 51. is_uploaded_file — 判断文件是否是通过 HTTP POST 上传的 52. is_writable — 判断给定的文件名是否可写 53. is_writeable — is_writable 的别名 54. lchgrp — 修改符号链接的所有组 55. lchown — 修改符号链接的所有者 56. link — 建立一个硬连接 57. linkinfo — 获取一个连接的信息 58. lstat — 给出一个文件或符号连接的信息 59. mkdir — 新建目录 60. move_uploaded_file — 将上传的文件移动到新位置 60. parse_ini_file — 解析一个配置文件 61. parse_ini_string — 解析配置字符串 62. pathinfo — 返回文件路径的信息 63. pclose — 关闭进程文件指针 64. popen — 打开进程文件指针 65. readfile — 输出文件 66. readlink — 返回符号连接指向的目标 67. realpath_cache_get — 获取真实目录缓存的详情 68. realpath_cache_size — 获取真实路径缓冲区的大小 69. realpath — 返回规范化的绝对路径名 70. rename — 重命名一个文件或目录 71. rewind — 倒回文件指针的位置 72. rmdir — 删除目录 73. set_file_buffer — stream_set_write_buffer 的别名 74. stat — 给出文件的信息 75. symlink — 建立符号连接 76. tempnam — 建立一个具有唯一文件名的文件 77. tmpfile — 建立一个临时文件 78. touch — 设定文件的访问和修改时间 79. umask — 改变当前的 umask 80. unlink — 删除文件 ## [Inotify](http://php.net/manual/zh/book.inotify.php "Inotify") ### Inotify 函数(5个) 类似 C 语言里的`inotify_init()` 函数会返回文件描述符,PHP 的 `inotify_init()`则返回 stream 资源。可以被标准的 Steam 函数使用,例如`stream_select()`、 `stream_set_blocking()`以及 `fclose()`。 `inotify_read()` 函数取代里 C 语言里读取 inotify 事件的那种方式。 1. inotify_add_watch — Add a watch to an initialized inotify instance 2. inotify_init — Initialize an inotify instance 3. inotify_queue_len — Return a number upper than zero if there are pending events 4. inotify_read — Read events from an inotify instance 5. inotify_rm_watch — Remove an existing watch from an inotify instance ## [Mimetype](http://php.net/manual/zh/book.mime-magic.php "Mimetype") This extension has been deprecated as the PECL extension [Fileinfo](http://php.net/manual/zh/book.fileinfo.php) provides the same functionality (and more) in a much cleaner way. ## [Phdfs](http://php.net/manual/zh/book.phdfs.php "Phdfs") ### phdfs类 1. phdfs::connect — Description 2. phdfs::__construct — Description 3. phdfs::copy — Description 4. phdfs::create_directory — Description 5. phdfs::delete — Description 6. phdfs::__destruct — Description 7. phdfs::disconnect — Description 8. phdfs::exists — Description 9. phdfs::file_info — Description 10. phdfs::list_directory — Description 11. phdfs::read — Description 12. phdfs::rename — Description 13. phdfs::tell — Description 14. phdfs::write — Description ## [Proctitle](http://php.net/manual/zh/book.proctitle.php "Proctitle") This extension allows changing the current process', and thread, name on Linux and \*BSD systems. This is useful when using [pcntl\_fork()](http://php.net/manual/zh/function.pcntl-fork.php) to identify running processes in process list. ### Proctitle 函数(2个) 1. setproctitle — Set the process title 2. setthreadtitle — Set the thread title ## [xattr](http://php.net/manual/zh/book.xattr.php "xattr") The xattr extension allows for the manipulation of extended attributes on a filesystem. ### xattr 函数(5个) 1. xattr_get — Get an extended attribute 2. xattr_list — Get a list of extended attributes 3. xattr_remove — Remove an extended attribute 4. xattr_set — Set an extended attribute 5. xattr_supported — Check if filesystem supports extended attributes ## [xdiff](http://php.net/manual/zh/book.xdiff.php "xdiff") xdiff extension enables you to create and apply patch files containing differences between different revisions of files. This extension supports two modes of operation - on strings and on files, as well as two different patch formats - unified and binary. Unified patches are excellent for text files as they are human-readable and easy to review. For binary files like archives or images, binary patches will be adequate choice as they are binary safe and handle non-printable characters well. ### xdiff 函数(18个) 1. xdiff_file_bdiff_size — Read a size of file created by applying a binary diff 2. xdiff_file_bdiff — Make binary diff of two files 3. xdiff_file_bpatch — Patch a file with a binary diff 4. xdiff_file_diff_binary — Alias of xdiff_file_bdiff 5. xdiff_file_diff — Make unified diff of two files 6. xdiff_file_merge3 — Merge 3 files into one 7. xdiff_file_patch_binary — Alias of xdiff_file_bpatch 8. xdiff_file_patch — Patch a file with an unified diff 9. xdiff_file_rabdiff — Make binary diff of two files using the Rabin's polynomial fingerprinting algorithm 10. xdiff_string_bdiff_size — Read a size of file created by applying a binary diff 11. xdiff_string_bdiff — Make binary diff of two strings 12. xdiff_string_bpatch — Patch a string with a binary diff 13. xdiff_string_diff_binary — Alias of xdiff_string_bdiff 14. xdiff_string_diff — Make unified diff of two strings 15. xdiff_string_merge3 — Merge 3 strings into one 16. xdiff_string_patch_binary — Alias of xdiff_string_bpatch 17. xdiff_string_patch — Patch a string with an unified diff 18. xdiff_string_rabdiff — Make binary diff of two strings using the Rabin's polynomial fingerprinting algorithm ## 备注1. php中怎么实现文件下载功能 1.直接做个超链接,地址为文件的地址 ~~~html <a href="文件地址">下载</a> ~~~ 说明:只能下载浏览器不能解析的文件,比如rar啊,脚本文件之类。如果文件是图片或者txt文档,就会直接在浏览器中打开。 2.流输出 ~~~php <?php $file=fopen('文件地址',"r"); header("Content-Type: application/octet-stream"); header("Accept-Ranges: bytes"); header("Accept-Length: ".filesize('文件地址')); header("Content-Disposition: attachment; filename=文件名称"); echo fread($file,filesize('文件地址')); fclose($file); ?> ~~~ 说明:输出的文件流,不存在上述问题,流输出后面不要再进行任何操作了 ## 备注2. PHP通过sql生成CSV文件并下载,PHP实现文件下载 ~~~php /** * PHP通过sql生成CSV文件并下载 * @param string $sql 查询sql,结果为二维数组 * @param array $title 数据,CSV文件标题 * @param boolean $retuen 是否只返回数据 * @param string $filename 文件名 */ function DownLoadCsvBySQL($sql,$title,$retuen=false,$filename='DownLoad.csv'){ $resoure = mysql_query($sql); $data = array(); if(!$resoure) exit('Select empty!'); while($rows = mysql_fetch_assoc($resoure)) $data[] = $rows; if($retuen) return $data; if(empty($data)) exit('data is empty'); ob_start(); $file = fopen("php://output", 'w'); fwrite($file,chr(0xEF).chr(0xBB).chr(0xBF)); fputcsv($file, $title); foreach($data as $k=>$v) fputcsv($file, $v); Header("Content-type: application/octet-stream"); #通过这句代码客户端浏览器就能知道服务端返回的文件形式 Header("Accept-Ranges: bytes"); #告诉客户端浏览器返回的文件大小是按照字节进行计算的 #Header("Accept-Length: ".filesize($filename)); #告诉浏览器返回的文件大小 Header("Content-Disposition: attachment; filename=".$filename); #告诉浏览器返回的文件的名称 echo fread($file,999999); fclose($file); } ~~~ ## 备注3. PHP实时生成并下载超大数据量的EXCEL文件 [文档链接](https://segmentfault.com/a/1190000011663425)