PHP 的 unlink 函数可以与路径一起使用吗?

发布于 2024-10-17 09:57:17 字数 202 浏览 2 评论 0原文

我想从 PHP 中的文件夹中删除文件,但我只有该文件的路径,是否可以提供取消链接的路径?例如

unlink('path/to/file.txt');

,如果这不起作用,删除这些文件的唯一方法是在 path/to/ 目录中创建一个 .php 文件,并以某种方式将其包含在我的文件中,并在那里调用一个方法来删除该文件,正确的?

I would like to remove a file from a folder in PHP, but I just have the path to this file, would it be ok to give the path to unlink? For example

unlink('path/to/file.txt');

If this doesn't work, the only way to get rid of those files would be to create a .php file in the path/to/ directory and include it somehow in my file an call a method there to remove the file, right?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

过去的过去 2024-10-24 09:57:17

有一个简单的方法来解决您的问题

使用此代码从文件夹中删除文件,

$_SERVER['DOCUMENT_ROOT']

该文件可以在取消链接函数中使用

工作代码

     unlink($_SERVER['DOCUMENT_ROOT'] . "/path/to/file.txt");

Got an easy method for your question

Use this code to remove a file from a folder

$_SERVER['DOCUMENT_ROOT']

this can be used inside the unlink function

worked code

     unlink($_SERVER['DOCUMENT_ROOT'] . "/path/to/file.txt");
寒冷纷飞旳雪 2024-10-24 09:57:17

查看 unlink 文档:

bool unlink ( string $filename [, Resource $context ] )

文件名
文件路径。

所以它采用字符串作为文件名。

确保可以通过执行脚本的位置的路径访问该文件。这不是绝对路径的问题,但相对路径可能会出现问题。

Have a look at the unlink documentation:

bool unlink ( string $filename [, resource $context ] )

and

filename
Path to the file.

So it only takes a string as filename.

Make sure the file is reachable with the path from the location you execute the script. This is not a problem with absolute paths, but you might have one with relative paths.

北风几吹夏 2024-10-24 09:57:17

取消链接可以很好地处理路径。

描述 bool unlink ( string
$文件名[,资源$上下文])

删除文件名。与Unix类似
C unlink() 函数。 E_WARNING 级别
失败时会产生错误。

文件名

文件路径。

如果遇到权限被拒绝错误的问题,有时是在您尝试删除工作目录层次结构中较高文件夹中的文件时引起的(即,当尝试删除以“../”开头的路径时) )。

因此,要解决此问题,您可以使用 chdir() 将工作目录更改为要取消链接的文件所在的文件夹。

<?php
    $old = getcwd(); // Save the current directory
    chdir($path_to_file);
    unlink($filename);
    chdir($old); // Restore the old working directory   
?>

unlink works fine with paths.

Description bool unlink ( string
$filename [, resource $context ] )

Deletes filename. Similar to the Unix
C unlink() function. A E_WARNING level
error will be generated on failure.

filename

Path to the file.

In case had a problem with the permissions denied error, it's sometimes caused when you try to delete a file that's in a folder higher in the hierarchy to your working directory (i.e. when trying to delete a path that starts with "../").

So to work around this problem, you can use chdir() to change the working directory to the folder where the file you want to unlink is located.

<?php
    $old = getcwd(); // Save the current directory
    chdir($path_to_file);
    unlink($filename);
    chdir($old); // Restore the old working directory   
?>
仅此而已 2024-10-24 09:57:17

不要忘记检查文件是否存在,否则如果不存在,您将收到错误消息:

$file_with_path = $_SERVER['DOCUMENT_ROOT'] . "/path/to/file.txt";
if (file_exists($file_with_path)) {
  unlink($file_with_path);
}

Don't forget to check if the file exists, or you will get an error if it doesn't:

$file_with_path = $_SERVER['DOCUMENT_ROOT'] . "/path/to/file.txt";
if (file_exists($file_with_path)) {
  unlink($file_with_path);
}
锦爱 2024-10-24 09:57:17

您可以将 unlink 与路径一起使用。

您还可以对目录执行取消链接,只要先清空该目录即可。

这是手册:http://php.net/manual/en/function.unlink。 php

You CAN use unlink with a path.

You can also perform unlink on a directory, as long as you have emptied it first.

Here is the manual: http://php.net/manual/en/function.unlink.php

山色无中 2024-10-24 09:57:17

根据文档,unlink 接受路径的字符串参数。

http://php.net/manual/en/function.unlink.php

换句话说...您拥有删除文件所需的一切。

According to the documentation, unlink accepts string parameter for the path.

http://php.net/manual/en/function.unlink.php

In other words... you have what you need to delete the file.

久随 2024-10-24 09:57:17

它不仅可以,而且是 PHP 中删除文件的唯一方法(除了系统调用之外)。

Not only is it OK, it is the only way to delete a file in PHP (besides system calls).

记忆里有你的影子 2024-10-24 09:57:17

我们可以使用这段代码

$path="images/all11.css";

if(unlink($path)) echo "Deleted file ";

We can use this code

$path="images/all11.css";

if(unlink($path)) echo "Deleted file ";
如梦 2024-10-24 09:57:17
if (isset($_POST['remove_file'])) {
           $file_path=$_POST['fileremove'];
     // chown($file_path, 'asif');
     // echo $file_path;
    if (file_exists($file_path)) {
          unlink($file_path);
        echo "file deleted<br> the name of file is".$file_path."";

        # code...
    }
    else
        echo "file is not deleted ".$file_path."";
    # code...
}
if (isset($_POST['remove_file'])) {
           $file_path=$_POST['fileremove'];
     // chown($file_path, 'asif');
     // echo $file_path;
    if (file_exists($file_path)) {
          unlink($file_path);
        echo "file deleted<br> the name of file is".$file_path."";

        # code...
    }
    else
        echo "file is not deleted ".$file_path."";
    # code...
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文