unlink($filename) : 权限被拒绝警告

unlink($filename) : Permission denied Warning

我在尝试使用 unlink() 时收到权限被拒绝的警告。

我创建了从内容目录中删除图像的函数:

function del($fnam){
chmod('content/'.$fnam, 0777);   
chown('content/'.$fnam,465);
unlink('content/'.$fnam);   
}

我在从论坛上阅读后使用了 chmod() 和 chown(),但我仍然收到警告并且图像没有被删除。

当我从不同的位置调用它时,它是如何运行的?

if($temp2==1){      //For Delete
$sql="delete from blog where b_id=$temp1";
$im=fetch('blog','b_img','b_id='.$temp1);

//Deleting Image

    del($im);
}

上面的代码没有任何功能(运行 程序方式),这给了我积极的回应。 如何解决?

尝试更改内容目录的权限

function del($fnam){
   chmod('content/, 0777);   
   unlink('content/'.$fnam);   
}

不幸的是,您的代码假设 chmodchown 将始终有效,这是一个错误的假设。

如果您没有正确的权限,它们也会失败。

像这样更改代码,它应该会告诉您实际出错的地方

function del($fnam){
    if ( ! chmod('content/'.$fnam, 0777) )  {
        echo 'chmod failed';
    }
    if ( ! chown('content/'.$fnam,465) ) {
        echo 'chown failed';
    }

    if ( ! unlink('content/'.$fnam) ) {  
        echo 'unlink failed';
    }

}

您没有得到预期的结果,因为:

根据手册chown:

Attempts to change the owner of the file filename to user user. Only the superuser may change the owner of a file.

只有当您的应用程序是 运行 作为 root 时它才会工作,我敢肯定情况并非如此。

根据手册chmod:

Attempts to change the mode of the specified file to that given in mode.

基本上,如果您没有正确的权限,chmodunlink 将不起作用。

在这种情况下,您无法使用 php 删除文件,因为如果您无法从上面更改权限,php 没有足够的权限,请尝试使用 FTP (我知道它不常见,但它的工作原理,它可以帮助您个人使用)

// set up basic connection
$conn_id = ftp_connect($ftp_server);

// login with username and password
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);

// try to delete $file
if (ftp_delete($conn_id, $file)) {
 echo "$file deleted successful\n";
} else {
 echo "could not delete $file\n";
}