首页  >  文章  >  后端开发  >  PHP删除文件

PHP删除文件

王林
王林原创
2024-08-29 13:02:571016浏览

每当需要在 PHP 中删除文件时,我们都会使用 PHP 中称为 unlink 函数的函数,该函数有两个参数,即文件名和上下文,其中文件名是要删除的文件所在的文件位置的路径已删除的位置,上下文表示文件句柄的上下文,它们是一组能够更改文件流行为的选项,并且取消链接函数的此参数是可选的,以防取消链接函数无法删除给定文件由文件路径指定,将生成错误消息。

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

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

语法:

unlink(path_of_the_file, context)

哪里,

  • path_of_the_file 是要删除的文件所在文件位置的路径。
  • context 表示文件句柄的上下文,它是一组能够更改文件流行为的选项,并且 unlink 函数的此参数是可选的。

PHP 中 Round 函数的工作原理

  • 每当需要在 PHP 中删除文件时,我们都会使用 PHP 中称为 unlink 函数的函数。
  • PHP 中的取消链接函数需要两个参数,即 file_of_the_file 和 context。
  • 参数path_of_the_file是要删除的文件所在文件位置的路径。
  • 参数 context 表示文件句柄的上下文,它是一组能够更改文件流行为的选项,并且该参数对于 unlink 函数是可选的。
  • 如果取消链接函数无法删除文件路径指定的给定文件,将会生成一条错误消息。

PHP 删除文件的示例

下面给出的是提到的示例:

示例#1

代码:

<html>
<body>
<?php
#a file is opened in write mode using fopen function and the contents are written to the file using fwrite function and then the file is closed using fclose function
$nameofthefile = fopen("filename.txt","w");
echo fwrite($nameofthefile,"Welcome to PHP");
fclose($nameofthefile);
#the file that was created is opened in read mode using fopen function and the contents are read from the file using fread function and displayed as the output on the screenand then the file is closed using fclose function
$fileread = fopen("filename.txt", "r");
echo fread($fileread, filesize("filename.txt"));
fclose($fileread);
#unlink function is used to delete the file that was just being read
unlink("filename.txt");
#again we try to read the contents of the file that was just deleted using unlink function
$fileread = fopen("filename.txt", "r");
echo fread($fileread, filesize("filename.txt"));
fclose($fileread);
?>
</body>
</html>

输出:

PHP删除文件

在上面的程序中,使用 fopen 函数以写入模式打开文件,并使用 fwrite 函数将内容写入文件,然后使用 fclose 函数关闭文件。然后使用 fopen 函数以读取模式打开同一文件,并使用 fread 函数读取文件的内容,并将文件的内容以及文件的大小作为输出显示在屏幕上,然后关闭文件使用 fclose 函数。然后我们使用 unlink 函数删除同一个创建的文件。然后,如果我们尝试再次读取已删除的文件,屏幕上会显示警告错误消息。输出如上面的快照所示。

示例#2

代码:

<html>
<body>
<?php
#a file is opened in write mode using fopen function and the contents are written to the file using fwrite function and then the file is closed using fclose function
$nameofthefile = fopen("filename.txt","w");
echo fwrite($nameofthefile,"Learning is fun");
fclose($nameofthefile);
#the file that was created is opened in read mode using fopen function and the contents are read from the file using fread function and displayed as the output on the screenand then the file is closed using fclose function
$filerad = fopen("filename.txt", "r");
echo fread($fileread, filesize("filename.txt"));
fclose($fileread);
#unlink function is used to delete the file that was just being read
unlink("filename.txt");
#again we try to read the contents of the file that was just deleted using unlink function
$fileread = fopen("filename.txt", "r");
echo fread($fileread, filesize("filename.txt"));
fclose($fileread);
?>
</body>
</html>

输出:

PHP删除文件

在上面的程序中,使用 fopen 函数以写入模式打开文件,并使用 fwrite 函数将内容写入文件,然后使用 fclose 函数关闭文件。然后使用 fopen 函数以读取模式打开同一文件,并使用 fread 函数读取文件的内容,并将文件的内容以及文件的大小作为输出显示在屏幕上,然后关闭文件使用 fclose 函数。然后我们使用 unlink 函数删除同一个创建的文件。然后,如果我们尝试再次读取已删除的文件,屏幕上会显示警告错误消息。输出如上面的快照所示。

结论

在本文中,我们通过示例及其输出了解了 unlink 函数的定义、语法、工作原理,了解了 PHP 中使用 unlink 函数删除文件的概念。

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

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn