Search code examples
phpunlink

unlink($filename) : Permission denied Warning


I am getting Permission denied waring when i try to use unlink().

I have created i function to delete an image from content directory :

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

I have used chmod() and chown() after reading from forums , still i am getting that warning and image is not getting deleted.

How ever it runs as charm when i call it from different location.

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

code above is not in any function (running procedural fashion), and this is giving me positive response. How to fix that?


Solution

  • in this situation you can't remove file with php because php don't have enough permission if you can't change permission from above , try a hack with FTP (i know its not usual but its works and it can help you for personal usage)

    // 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";
    }