1

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

gotdalife 在 gmail dot com 25-Sep-2008 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]: 第 37 行的 J:\xampp\htdocs\bunker\admin\delete_file.php 中没有错误

第 37 行是:

unlink($filename);

谁能看到我做错了什么?

4

2 回答 2

2

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

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

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

于 2010-11-15T14:30:34.070 回答
0

看这里:

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

和这里

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

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

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

于 2010-11-15T14:30:50.067 回答