gpt4 book ai didi

php - 先删除文件再删除行数据?

转载 作者:行者123 更新时间:2023-11-29 06:37:23 24 4
gpt4 key购买 nike

我想在从数据库中删除行数据之前从上传文件夹中删除该文件。我使用了下面的代码,但它给出了错误。错误=>没有这样的文件或目录

function deleteItem($conn,$product_id)
{
$stmtgetfile=$conn->prepare("SELECT * FROM tbl_item WHERE product_id=:product_id");
$stmtgetfile->bindParam('product_id',$product_id);
$stmtgetfile->execute();
$row = $stmtgetfile->fetch(PDO::FETCH_ASSOC);
$item=$row['product_photo'];
$path="../uploads/".$item;
unlink($path);
// $stmtdelete=$conn->prepare("DELETE FROM tbl_item WHERE product_id=:product_id");
// $stmtdelete->bindParam('product_id',$product_id);
// if($stmtdelete->execute())
// return true;
// return false;
}

最佳答案

使用$_SERVER['DOCUMENT_ROOT']获取根目录的绝对路径。

$path=$_SERVER['DOCUMENT_ROOT']."/uploads/".$item;
if(file_exists($path)){
unlink($path);
}else{
echo $path; // check path here
}

关于php - 先删除文件再删除行数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53184518/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com