文件不会使用unlink删除

时间:2012-04-13 21:32:09

标签: php mysql delete-file unlink

下面的文件应该删除存储在文件夹中的文件和与该文件相关的数据库行。数据库行删除工作正常,但我无法删除文件。 doc_link是一个表列,用于存储图像的相对路径。任何帮助将不胜感激。

代码     

$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>';
}
?>

1 个答案:

答案 0 :(得分:4)

您不能只运行mysql_query()并期望它返回该值doc_link。您还必须运行mysqli_fetch_array()获取该行,然后通过$row['doc_link']访问该值。 $pathresult是MySQL资源,而不是文件的(String)路径。