Search code examples
phpmysqldelete-fileunlink

File wont delete using unlink


The file below is supposed to delete both a file stored in a folder and the database row relating to that file. The database row deletion works fine, but I am unable to get the file deletion to work. The doc_link is a table column that stores the relative path of the image. Any help would be greatly appreciated.

The Code

$delete = $_POST['checkbox'];

foreach ($delete as $id => $val) {
    //Get file path stored in table and delete file
    $relpath="SELECT doc_link FROM documents WHERE id = '".$id."'";
    $pathresult= mysqli_query($con, $relpath) or die("Invalid query");
    unlink($pathresult);

    //Deletes row from table
    $query="DELETE FROM documents WHERE id = '".$id."'";
    $result= mysqli_query($con, $query) or die("Invalid query"); 
}

//Show that the items have been successfully removed.//
if (mysqli_affected_rows($con) > 0) {
echo '<p>The selected items have been successfully deleted.</p>';
} else {
echo '<p>An error has occurred while processing your request</p>';
}
?>

Solution

  • You can't just run mysql_query() and expect it to return that value doc_link. You also have to run mysqli_fetch_array() to fetch the row, then access that value via $row['doc_link']. $pathresult is a MySQL resource, not a (String) path to a file.