我在localhost phpmyadmin上有一个数据库,当我将图像上传到数据库时,它工作正常(它插入数据库并存储在文件夹中);但是,当我尝试删除图像时,数据库中的记录将被删除,但图像不会从该文件夹中删除。 我把unlink放在错误的地方了吗?
存储到文件夹的代码:
$alphabet = "abcdefghijklmnopqrstuwxyzABCDEFGHIJKLMNOPQRSTUWXYZ";
$pass = array(); //remember to declare $pass as an array
$alphaLength = strlen($alphabet) - 1; //put the length -1 in cache
for ($i = 0; $i < 6; $i++)
{
$n = rand(0, $alphaLength);
$pass[] = $alphabet[$n];
}
$random = implode($pass);
$imageName = $random.$_FILES['file']['name'];
move_uploaded_file($_FILES['file']['tmp_name'],'../angelimg/'.$imageName);
点击删除按钮时的代码:
if(isset($_POST['imgDelete']))
{
$imgID = trim($_POST['imgID']);
include("../../services/databaseConnection.php");
if($stmt = mysqli_prepare($mysqli, "DELETE FROM ServiceImage WHERE imgID = ?"))
{
//bind
mysqli_stmt_bind_param($stmt, "i", $imgID);
//unlink image from folder
unlink('../angelimg/'.$imageName);
//execute
mysqli_stmt_execute($stmt) or die (mysqli_error($mysqli));
//close
mysqli_stmt_close($stmt);
}
//close db
mysqli_close($mysqli);
//direct user to confirmation page of successful image upload
//echo("check directory");
Header("Location: Confirmation.php");
}