京东6.18大促主会场领京享红包更优惠

 找回密码
 立即注册

QQ登录

只需一步,快速开始

一文带你掌握PHP中常见的文件操作

2024-11-4 06:30| 发布者: db4d5a85| 查看: 88| 评论: 0

摘要: 目次一、文件读取的5种方法二、文件写入三、文件复制、删除、重命名一、文件读取的5种方法 1、file_get_contents: 将整个文件读入一个字符串 [code]file_get_contents(string $filename,bool $use_include_path = fa
目次

一、文件读取的5种方法

1、file_get_contents: 将整个文件读入一个字符串

[code]file_get_contents(string $filename,bool $use_include_path = false,?resource $context = null,int $offset = 0,?int $length = null): string|false[/code]

可以读取本地的文件

也可以用来打开一个网络地址实现简朴的网页抓取

可以模拟post请求(stream_context_create)

[code]$fileName = 'test.txt'; if (file_exists($fileName)) { $str = file_get_contents($fileName); echo $str; } else { print_r("文件不存在"); } [/code]

2、file: 把整个文件读入一个数组中

[code]file(string $filename, int $flags = 0, ?resource $context = null): array|false[/code]

数组的每个元素对应于文件中的一行

可以读取本地的文件

也可以用来读取一个网络文件

[code]$lines = file('test.txt'); foreach ($lines as $line_num => $line) { echo "Line #<b>{$line_num}</b> : " . htmlspecialchars($line) . "<br />\n"; } [/code]

3、file_open、file_gets、file_read、fclose: 从文件指针资源读取

3.1 fgets — 从文件指针中读取一行

[code]fgets(resource $stream, ?int $length = null): string|false[/code]

从文件中读取一行并返回长度最多为 length - 1 字节的字符串。遇到换行符(包罗在返回值中)、EOF 大概已经读取了 length - 1 字节后制止(看先遇到那一种环境)。如果没有指定 length,则默认为 1K,大概说 1024 字节。

[code]$fp = fopen("test.txt", "r"); if ($fp) { while (!feof($fp)) { $line = fgets($fp); echo $line . "<br />\n"; } fclose($fp); } [/code]

3.2 fread — 从文件指针中读取固定长度(可安全用于二进制文件)

[code]fread(resource $stream, int $length): string|false[/code] [code]$fp = fopen("test.txt", "r"); if ($fp) { $content = ""; while (!feof($fp)) { $content .= fread($fp, 1024); } #$contents = fread($handle, filesize($filename)); echo $content; fclose($fp); } [/code]

4、SplFileObject 类

https://www.php.net/manual/zh/class.splfileobject.php

5、调用linux下令

处置处罚超大文件,比如日记文件时,可以用fseek函数定位,也可以调用linux下令处置处罚

[code]$file = 'access.log'; $file = escapeshellarg($file); // 对下令行参数进行安全转义 $line = `tail -n 1 $file`; echo $line; [/code]

二、文件写入

1、file_put_contents: 将数据写入文件

[code]file_put_contents(string $filename,mixed $data,int $flags = 0,?resource $context = null): int|false[/code] [code]$content = "Hello, world!"; // 要写入文件的内容 $file = "test.txt"; // 文件路径 file_put_contents($file, $content); [/code]

2、fwrite: 写入文件(可安全用于二进制文件)

[code]fwrite(resource $stream, string $data, ?int $length = null): int|false[/code] [code]$content = "这是要写入文件的内容"; $file = fopen("test.txt", "w"); // 打开文件写入模式 if ($file) { fwrite($file, $content); // 将内容写入文件 fclose($file); // 关闭文件 } [/code]

3、SplFileObject 类

三、文件复制、删除、重命名

1、copy: 拷贝文件

[code]copy(string $from, string $to, ?resource $context = null): bool[/code] [code]$file = 'test.txt'; $newFile = 'test2.txt'; if (!copy($file, $newFile)) { echo "failed to copy $file...\n"; } [/code]

2、unlink: 删除文件

[code]unlink(string $filename, ?resource $context = null): bool[/code] [code]$fileName = 'test2.txt'; if (file_exists($fileName)) { if (unlink($fileName)) { echo '删除乐成'; } } [/code]

3、rename: 重命名文件

[code]rename(string $from, string $to, ?resource $context = null): bool[/code]

可以在差别目次间移动

如果重命名文件时 to 已经存在,将会覆盖掉它

如果重命名文件夹时 to 已经存在,本函数将导致一个告诫

[code]$fileName = 'test.txt'; $rename = 'test_new.txt'; if (file_exists($fileName)) { if (rename($fileName, $rename )) { echo '重命名乐成'; } }[/code]

到此这篇关于一文带你掌握PHP中常见的文件操作的文章就介绍到这了,更多相干PHP文件操作内容请搜索脚本之家以前的文章或继续欣赏下面的相干文章渴望大家以后多多支持脚本之家!


来源:https://www.jb51.net/program/317532mhm.htm
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!
关闭

站长推荐上一条 /6 下一条

QQ|手机版|小黑屋|梦想之都-俊月星空 ( 粤ICP备18056059号 )|网站地图

GMT+8, 2025-7-2 02:43 , Processed in 0.031128 second(s), 18 queries .

Powered by Mxzdjyxk! X3.5

© 2001-2025 Discuz! Team.

返回顶部