php文件未取消链接

php文件未取消链接,php,permissions,directory,unlink,Php,Permissions,Directory,Unlink,我正在调用位于“uploads”目录中的.php文件,以便从文件系统中删除。需要搜索的目录将始终位于“uploads/$\u SESSION['email']”内。这就是我目前拥有的: <?php session_start(); require('../mysqli_connect.php'); $old = getcwd(); // Save the current directory $new = './'.$_SESSION['email']; c

我正在调用位于“uploads”目录中的.php文件,以便从文件系统中删除。需要搜索的目录将始终位于“uploads/$\u 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); 

}

    }


?>



我在这里看到一些问题。。。第一个是$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);

    }

?>

我在这里看到了几个问题。。。第一个是$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);

    }

?>

Dude您正在a侧循环中定义一个函数,而不是调用它…我正在从另一个php文件调用它您正在取消从数据库检索的文件的链接。Dude您正在a侧循环中定义一个函数,而不是调用它…我正在从另一个php文件调用它您正在取消从数据库检索的文件的链接谢谢。非常彻底的回答,这对现在的工作有很大帮助。谢谢。非常彻底的回答,这对现在的工作有很大帮助。