【问题标题】:php file not unlinkingphp文件没有取消链接
【发布时间】:2012-08-05 23:43:56
【问题描述】:

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

}

    }


?>

【问题讨论】:

  • 老兄,你在循环中定义一个函数而不是调用它......
  • 我从另一个 php 文件调用它
  • 您正在取消链接从数据库中检索到的文件显然不正确

标签: php permissions directory unlink


【解决方案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);
    }
?>

【讨论】:

    【解决方案2】:

    我在这里看到了几个问题...第一个是 $row 将作为数组传回,所以简单地调用 $row 不会给你想要的结果(我'm 假设是您尝试删除的文件的名称,该文件存储在您的数据库中)。 $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);
    
        }
    
    ?>
    

    【讨论】:

    • 谢谢。非常彻底的答案,这很有帮助 - 现在可以工作了。
    猜你喜欢
    • 2017-12-15
    • 2012-02-02
    • 2011-11-14
    • 1970-01-01
    • 2016-10-04
    • 2012-02-02
    • 2017-05-06
    • 1970-01-01
    • 2023-03-08
    相关资源
    最近更新 更多