0

我正在调用位于我的“上传”目录中的 .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); 

}

    }


?>
4

2 回答 2

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);

    }

?>
于 2012-08-08T19:44:49.203 回答
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);
    }
?>
于 2012-08-08T19:40:29.203 回答