Php 简明教程

PHP - Delete File

PHP 没有 delete 关键字或 delete() 函数。它提供 unlink() 函数代替,此函数被调用时会从文件系统中删除一个文件。它类似于 Unix/C 中的 unlink 函数。

PHP doesn’t have either a delete keyword or a delete() function. Instead, it provides the unlink() function, which when called, deletes a file from the filesystem. It is similar to Unix/C unlink function.

如果无法完成删除操作,则 PHP 会返回 false 并显示一条 E_WARNING 消息。

If the delete operation could not be completed, PHP returns false and shows an E_WARNING message.

unlink(string $filename, ?resource $context = null): bool

unlink() 函数的必需字符串参数是一个字符串,该字符串指明要删除的文件。

The mandatory string parameter to unlink() function is a string that refers to the file to be deleted.

Example

以下代码演示了 unlink() 函数的简单用法:

The following code demonstrates a simple use of the unlink() function −

<?php
   $file = "my_file.txt";

   if (unlink($file)) {
      echo "The file was deleted successfully.";
   } else {
      echo "The file could not be deleted.";
   }
?>

unlink() 函数还可以删除到某个文件符号链接。但是,删除符号链接并不会删除原始文件。符号链接是既存文件的一个捷径。

The unlink() function can also delete a symlink to a file. However, deleting a symlink doesn’t delete the original file. A symlink is a shortcut to an existing file.

在 Windows 中,以管理员权限打开命令提示符,并使用 mlink 命令和 /h 开关来创建到某个文件的符号链接。( /j 开关用于目录的符号链接)

In Windows, open a command prompt with administrator privilege and use the mlink command with /h switch to create a symlink to a file. (/j switch is used for symlink to a folder)

mklink /h hellolink.lnk hello.txt
Hardlink created for hellolink.lnk <<===>> hello.txt

在 Ubuntu Linux 中,要创建到某个文件的符号链接,可使用以下命令:

In Ubuntu Linux, to create a symbolic link to a file, you would use the following command −

ln -s /path/to/original_file /path/to/symlink

要创建到某个目录的符号链接,可使用以下命令:

To create a symbolic link to a directory, you would use the following command −

ln -s /path/to/original_directory /path/to/symlink

在 PHP 中,还有一个 symlink() 函数用于此目的。

In PHP, there is also a symlink() function for the purpose.

symlink(string $target, string $link): bool

Example

使用以下代码创建符号链接:

Create a symlink with the following code −

<?php
   $target = 'hello.txt';
   $link = 'hellolink.lnk';
   symlink($target, $link);

   echo readlink($link);
?>

现在,删除上面创建的符号链接:

Now delete the symlink created above −

unlink("hellolink.lnk");

如果查看当前工作目录,则符号链接将被删除,而原始文件保持不变。

If you check the current working directory, the symlink will be deleted, leaving the original file intact.

How to Rename a File in PHP

您可借助操作系统的控制台中相应命令来更改现有文件的名称。例如,Linux 终端中的“ mv command ”或 Windows 命令提示符中的“ rename command ”可帮助您更改某个文件的名称。

You can change the name of an existing file with the help of respective command from the console of an operating system. For example, the "mv command in Linux terminal or the "rename command" in Windows command prompt helps you to change the name of a file.

但是,要以编程方式重命名文件,PHP 的内置库中包含一个 rename() 函数。

However, to rename a file programmatically, PHP’s built-in library includes a rename() function.

以下是 rename() 函数的 syntax -

Here is the syntax of the rename() function −

rename(string $from, string $to, ?resource $context = null): bool

$from$to 两个字符串分别是文件的名字,文件已经存在和新的。rename() 函数会尝试将 $from 重命名为 $to ,如果需要的话,它会将其移动到其他目录。

Both $from and $to strings are the names of files, existing and new respectively. The rename() function attempts to rename $from to $to, moving it between directories if necessary.

如果你正在重命名 file$to 已存在,那么 $to 将被覆写。如果你正在重命名 directory$to 存在,那么这个函数将抛出一个 warning

If you are renaming a file and $to already exists, then it will be overwritten. If you are renaming a directory and $to exists, then this function will emit a warning.

将“hello.txt”重命名为“test.txt” -

To change the name of "hello.txt" to "test.txt" −

<?php
   rename("hello.txt", "test.txt");
?>

你也可以使用一个间接的方法来重命名文件。复制一个已存在的文件,并且删除原件。这样也可以将“hello.txt”重命名为“test.txt” -

You can also employ a little indirect approach for renaming a file. Make a copy of an existing file and delete the original one. This also renames "hello.txt" to "test.txt" −

copy("hello.txt", "test.txt");
unlink("hello.txt");