php文件没有取消链接

时间:2012-08-08 19:25:37

标签: php permissions directory unlink

我正在调用位于我的'uploads'目录中的.php文件,以便从文件系统中删除。需要搜索的目录将始终位于“uploads / $ _ SESSION ['email']”中。这就是我目前所拥有的:

<?php

session_start();

require('../mysqli_connect.php'); 


    $old = getcwd(); // Save the current directory
    $new = './'.$_SESSION['email'];
    chdir($new);

    $delpaths = "SELECT `title` FROM `upload` WHERE `upload_id` IN ({$id_array}) AND `owner_id` =". $_SESSION['user_id'];
    $getpaths = mysqli_query($dbc, $delpaths);
    while ($row = mysqli_fetch_array($getpaths, MYSQLI_ASSOC))
    {
        chown($row, 666);

function removeupload() {
$todelete = $row;
unlink($todelete);
chdir($old); 

}

    }


?>

2 个答案:

答案 0 :(得分:2)

我在这里看到了几个问题...第一个是$ row将作为数组传回,所以只是调用$ row将不会给你想要的结果(我假设是您尝试删除的文件的名称,该文件存储在您的数据库中。 $ row应更改为:

chown($row['column_name'], 666); // Where column_name is the name of the file you're trying to delete

第二个是没有任何内容传递给你的功能。你应该重写它,以便你可以传递几个变量,以便它正确执行,我们将添加$ filename和$ old。

function removeupload($filename, $old) {
    unlink($filename);
    chdir($old); 
}    

最后,您应该在while循环之外定义此函数,通常在页面的开头,并在您希望发生所需效果时调用它。

您的最终代码应该看起来像这样。

<?php
    //Start the session
    session_start();

    //Include the connection script
    require('../mysqli_connect.php'); 

    //Build the file deletion function
    function removeupload($filename, $old) {
        unlink($filename);
        chdir($old); 
    }  

    $old = getcwd(); // Save the current directory
    $new = './'.$_SESSION['email'];
    chdir($new);

    $delpaths = "SELECT `title` FROM `upload` WHERE `upload_id` IN ({$id_array}) AND `owner_id` =". $_SESSION['user_id'];
    $getpaths = mysqli_query($dbc, $delpaths);

    while ($row = mysqli_fetch_array($getpaths, MYSQLI_ASSOC)) {
        chown($row['column_name'], 666); //This $row column holds the file name, whatever you've called it in your database.

        //Now call the function and pass the variables to it
        removeupload($row['column_name'], $old);

    }

?>

答案 1 :(得分:1)

<?php

session_start();

require('../mysqli_connect.php'); 

function removeupload($row , $old){
    $todelete = $row;
    unlink($todelete);
    chdir($old);
}

    $old = getcwd(); // Save the current directory
    $new = './'.$_SESSION['email'];
    chdir($new);

    $delpaths = "SELECT `title` FROM `upload` WHERE `upload_id` IN ({$id_array}) AND `owner_id` =". $_SESSION['user_id'];
    $getpaths = mysqli_query($dbc, $delpaths);
    while ($row = mysqli_fetch_array($getpaths, MYSQLI_ASSOC))
    {
        chown($row, 666);
        removeupload($row['title'] , $old);
    }
?>