删除上级目录下的文件

发布于 2024-10-02 17:07:38 字数 1480 浏览 1 评论 0原文

我在从更高的目录中删除文件时遇到问题,我找到了这篇文章并尝试了它,但没有运气......:

gotdalife 在 gmail dot com 2008 年 9 月 25 日 02:04

致所有遇到问题的人 权限被拒绝错误,它是 有时当您尝试时会导致 删除文件夹中的文件 在你的层次结构中处于更高的位置 工作目录(即当尝试 删除以“../”开头的路径)。

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

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

这是我当前拥有的代码:

session_start();

if (!isset($_SESSION['agent']) OR ($_SESSION['agent'] !=md5($_SERVER['HTTP_USER_AGENT']))){

    require_once ('includes/login_functions.inc.php');
    $url = absolute_url();
    header("Location: $url");
    exit();
}  



$folder = $_GET['folder'];
$filename = $_GET['name'];
$path = "../gallery/photos/$folder";

if (isset($_POST['submitted'])) {

    if ($_POST['sure'] == 'Yes') {  

        $old = getcwd(); // Save the current directory
        chdir($path);
        unlink($filename);
        chdir($old); // Restore the old working directory  

    }
    else{

        echo '<p>The photo has NOT been deleted.</p>';
    }
}

我收到错误消息:

警告:unlink() [function.unlink]: 没有错误 J:\xampp\htdocs\bunker\admin\delete_file.php 第 37 行

第 37 行是:

unlink($filename);

有人能看到我做错了什么吗?

I'm having problems deleting a file from a higher directory, I found this post and tried it but no luck....:

gotdalife at gmail dot com 25-Sep-2008
02:04

To anyone who's 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     ?>

here is the code that I currently have:

session_start();

if (!isset($_SESSION['agent']) OR ($_SESSION['agent'] !=md5($_SERVER['HTTP_USER_AGENT']))){

    require_once ('includes/login_functions.inc.php');
    $url = absolute_url();
    header("Location: $url");
    exit();
}  



$folder = $_GET['folder'];
$filename = $_GET['name'];
$path = "../gallery/photos/$folder";

if (isset($_POST['submitted'])) {

    if ($_POST['sure'] == 'Yes') {  

        $old = getcwd(); // Save the current directory
        chdir($path);
        unlink($filename);
        chdir($old); // Restore the old working directory  

    }
    else{

        echo '<p>The photo has NOT been deleted.</p>';
    }
}

I'm getting the error message :

Warning: unlink() [function.unlink]:
No error in
J:\xampp\htdocs\bunker\admin\delete_file.php
on line 37

line 37 being:

unlink($filename);

can anybody see what I've done wrong?

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

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

发布评论

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

评论(2

葬花如无物 2024-10-09 17:07:38

我总是使用绝对文件路径名。

我将 filedir 定义为配置中的常量,然后连接以便获得绝对文件路径,然后调用 unlink()。

顺便说一句:我希望您知道您的代码高度不安全。

I always use absolute filepath names.

I'd define the filedir as a constant in your config, then concatenate so you have an absolute filepath, then make a call to unlink().

Btw: I hope you know your code is highly insecure.

怀中猫帐中妖 2024-10-09 17:07:38

请参阅此处:

http://bugs.php.net/bug.php?id=43511< /a>

和这里

http://php.bigresource.com/Track-php-03TimDKO/< /a>

http://www.phpbuilder.com/board/showthread.php? t=10357994

虽然根据上面的评论,我不建议这样做。是否可以选择不同的方法?

See here:

http://bugs.php.net/bug.php?id=43511

and here

http://php.bigresource.com/Track-php-03TimDKO/

http://www.phpbuilder.com/board/showthread.php?t=10357994

Though I wouldnt recommend doing this, as per the comments above. Is there the option for a different approach?

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