PHP 文件处理

王林
发布: 2024-08-29 13:06:58
原创
737 人浏览过

所有现代软件都需要与文件交互。它们要么需要接受文件形式的输入,要么生成输出并将其添加到文件中。无论哪种情况,与文件集成的功能都已成为几乎所有用于运营业务的软件的不可或缺的功能。对于任何应用程序来说,文件的处理都是必要的。必须处理该文件才能执行某些任务。 PHP 中的文件处理类似于任何语言(例如 C)的文件处理。PHP 有许多普通的文件函数可以使用。

开始您的免费软件开发课程

网络开发、编程语言、软件测试及其他

PHP 文件处理能力的用例

例如,银行需要软件来帮助他们生成 3 个月或 6 个月期间的银行账户对账单等报告,电子商务公司需要打印与库存和销售相关的报告,最后但并非最不重要的一点与股票市场交易相关的应用程序需要以可读文件的形式提供每日股票价格。我确信通过这个例子,您会同意任何支持业务功能的软件都需要您读取数据或将数据写入文件。

由于文件处理能力在现代应用程序中几乎是必需的,因此所有著名的编程语言(例如 Python、Java、C# 和 PHP)都提供了内置的文件处理功能,开发人员可以利用这些功能来开发交互式应用程序。

PHP 中的文件处理功能

PHP 支持以下文件格式的读写操作。

  • 文本文件: 扩展名为 .txt 的文件
  • 日志文件: 扩展名为 .log 的文件
  • 自定义扩展名: 具有自定义扩展名(如 .abc)的文件
  • CSV 文件: 扩展名为 .csv 的文件
  • 图像文件: 扩展名为 .jpg/png/gif 的文件
  • 具有初始化设置的文件: 扩展名为 .ini 的文件

PHP 中的文件处理函数

PHP 提供了广泛的内置函数来执行各种文件操作。这些文件函数适用于所有操作系统,例如 Linus、Unix、MAC 和 Windows。但是,MAC OS 和 Windows 中的文件名不区分大小写,而 Unix 和 Linux 中的文件名区分大小写。因此,为了避免任何混淆或错误,最好的做法是用小写字母命名所有文件,因为它可以确保完整的平台兼容性。

现在我们已经对 php 文件处理函数的工作原理有了较高的了解,让我们一一了解这些函数。

1. file_exists() 函数

该函数用于验证作为参数提供给它的文件名是否存在。它用于避免由于尝试读取或写入不存在的文件而可能导致的错误。

语法:

<?php
file_exists($file_name) //where file_name  would be a file with one of the supported extensions
?>
登录后复制

如果文件存在,file_exists() 将返回 True 值,如果文件不存在,则返回 false。

现在让我们在代码规范中使用此函数来检查文件是否存在。让我们在根文件夹中放置一个名为“mysettings.ini”的文件,并尝试使用以下代码访问它。

代码:

<?php
if (file_exists('mysettings.ini))
{
echo 'yay! file found!';
}
else
{
echo 'Sorry! mysettings.ini does not exist';
}
?>
登录后复制

输出:

PHP 文件处理

现在,如果我们从该位置删除文件并运行上面的代码,我们将看到以下输出。

PHP 文件处理

2. fopen() 函数

fopen 函数在 php 中用于打开要由应用程序读取的文件。

语法:

<?php
fopen($fname,$mode,$use_include_path,$context);
?>
登录后复制

在上面的语法中,$fname代表文件名,$mode代表我们要打开文件的模式。 $mode 可以是以下值之一。

  • r: For opening the file in only read-only mode. It returns false if the file name supplied is not found on the location supplied.
  • r+: For opening the file in both read and write mode. Similar to ‘r’, it also returns false if a file is not found.
  • w: For opening the file in only write-only mode. If the file supplied doesn’t exist, it attempts to create one.
  • w+: For opening the file in both read and write mode. Similar to ‘w’, it also attempts to create a file if the file name supplied is not found.
  • a: For opening the file in write-only mode and appending to the end of the file. If the file supplied doesn’t exist, it attempts to create one.
  • a+: For opening the file in both read and write mode. Similar to ‘a’, it also attempts to create a file if the file name supplied is not found.

3. fwrite() Function

As the name suggests, this function is used to write content to files.

Syntax:

<?php
fwrite($handle, $data_string, $len);
?>
登录后复制

Where $handle is the file location, $data_string is the text string we would like to write to the file and $len is the optional parameter to specify the maximum length of the file.

4. fclose() Function

The fclose() function is used in php when the read/write operations on file are completed and we would like to close the file.

Syntax:

<?php
fclose($file_handle);
?>
登录后复制

Where $file_handle stands for the file pointer.

5. fgets() Function

The fgets() function is used in php to read the file line by line.

Syntax:

<?php
fgets($file_handle);
?>
登录后复制

Where $file_handle stands for the file pointer.

6. copy() Function

The copy() function allows us to copy a file in php.

Syntax:

<?php
copy($file1, $file2);
?>
登录后复制

Where $file1 is the original file and $file2 is the copied file location.

7. unlink() Function

The unlink() function in Php is used to delete a file.

Syntax:

<?php
unlink($filename);
?>
登录后复制

Where the $filename is the filename to be deleted.

Conclusion

With the above example, we can easily conclude that php has a wide variety of in-built functions that simplify reading and writing operations on the file. The most commonly used function include fopen() to open the file in different modes, fwrite() to write data to the file, fread() to read the file content, fclose() to close the file once the necessary operation is done, copy() to copy one file content to another and unlink to delete the unwanted files.

以上是PHP 文件处理的详细内容。更多信息请关注PHP中文网其他相关文章!

相关标签:
php
来源:php
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!