PHP更新分类上传新图时unlink()无法删除旧图片问题求助
问题定位
unlink() 无法删除旧图片的核心错误是路径拼接缺失目录分隔符:
在code.php的更新逻辑里,你判断旧文件是否存在时用的路径是正确的:
if(file_exists("../uploads/".$old_image))
这行拼接出的路径格式为../uploads/旧文件名.jpg,和你实际存储图片的路径一致。但紧接着执行删除的代码漏写了uploads和文件名之间的/:
unlink("../uploads".$old_image);
这行实际拼接出的路径是../uploads旧文件名.jpg,程序会去上级目录找一个名字以uploads开头、后面直接跟旧图片名的文件,根本定位不到uploads目录里存的旧图,自然删除失败。
修复方案
直接把unlink那行的路径补全斜杠即可,修改后对应代码段:
if($_FILES['image']['name'] != "") { move_uploaded_file($_FILES['image']['tmp_name'], $path.'/'.$update_filename); if(file_exists("../uploads/".$old_image)) { unlink("../uploads/".$old_image); } }
其他优化建议
- 调整执行顺序:现有逻辑是先更新数据库里的图片字段,再执行新图移动、旧图删除操作。如果后续移动新文件出错,数据库里已经存了新文件名,会导致前台图片加载404。建议改成先移动新上传的图片,确认移动成功后再更新数据库、删除旧图片。
- 补全安全校验:现有SQL直接拼接POST传入的参数,存在SQL注入风险,建议改用mysqli预处理语句绑定参数执行数据库操作。
- 删除文件前增加非空判断:如果旧图片字段值为空,不要执行unlink操作,避免误删服务器文件。
附相关代码文件参考
edit-category.php
<?php include('includes/header.php'); include('../middleware/adminMiddleware.php'); ?> <!-- Add cards for better UI --> <div class="container"> <div class="row"> <div class="col-md-12"> <?php if(isset($_GET['id'])) { $id = $_GET['id']; $category = getByID("categories", $id); if(mysqli_num_rows($category) > 0) { $data = mysqli_fetch_array($category); ?> <div class="card"> <div class="card-header"> <h4>Edit Category </h4> </div> <div class="card-body"> <form action="code.php" method="POST" enctype="multipart/form-data"> <div class="row"> <div class="col-md-6"> <label for="">Name</label> <input type="hidden" name="category_id" value="<?= $data['id'] ?>" class="form-control" placeholder="Enter Category Name" > <input type="text" name="name" value="<?= $data['name'] ?>" class="form-control" placeholder="Enter Category Name" > </div> <div class="col-md-6"> <label for="">Slug</label> <input type="text" name="slug" value="<?= $data['slug'] ?>" class="form-control" placeholder="Enter Slug"> </div> <div class="col-md-12"> <label for="">Description</label> <textarea rows="3" name="description" class="form-control" placeholder="Enter Description"> <?= $data['description'] ?></textarea> </div> <div class="col-md-12"> <label for="">Upload Image</label> <input type="file" name="image" class="form-control"> <label for="">Current Image</label> <input type="hidden" name="old_image" value="<?= $data['image'] ?>"> <img src="../uploads/<?= $data['image'] ?>" alt="" height="100px" width="100px"> </div> <div class="col-md-12"> <label for="">Meta Title</label> <input type="text" name="meta_title" value="<?= $data['meta_title'] ?>" class="form-control" placeholder="Enter Title"> </div> <div class="col-md-12"> <label for="">Meta Description</label> <textarea rows="3" name="meta_description" class="form-control" placeholder="Enter Meta Description"><?= $data['meta_description'] ?></textarea> </div> <div class="col-md-12"> <label for="">Meta Keywords</label> <textarea rows="3" name="meta_keywords" class="form-control" placeholder="Enter Keywords"><?= $data['meta_keywords'] ?></textarea> </div> <div class="col-md-6"> <label for="">Status</label> <input type="checkbox" <?= $data['status'] ? "checked":"" ?>name="status"> </div> <div class="col-md-6"> <label for="">Popular</label> <input type="checkbox" <?= $data['popular'] ? "checked":"" ?> name="popular"> </div> <div class="col-md-12"> <button type="submit" class="btn btn-primary" name="update_category_btn"> Update </button> </div> </div> </form> </div> </div> <?php } else { echo "Category not found"; } ?> <?php } else { echo "Something went wrong"; } ?> </div> </div> </div> <?php include('includes/footer.php')?>
code.php(对应更新分类逻辑段)
else if(isset($_POST['update_category_btn'])) { $category_id = $_POST['category_id']; $name = $_POST['name']; $slug = $_POST['slug']; $description = $_POST['description']; $meta_title = $_POST['meta_title']; $meta_description = $_POST['meta_description']; $meta_keywords = $_POST['meta_keywords']; $status = isset($_POST['status']) ? '1':'0' ; $popular = isset($_POST['popular']) ? '1':'0' ; // 上传新图片变量 $new_image = $_FILES['image']['name']; // 存储旧图片名 $old_image = $_POST['old_image']; // 新图片不为空则生成新文件名 if($new_image != "") { $image_ext = pathinfo($new_image, PATHINFO_EXTENSION); $update_filename = time().'.'.$image_ext; } // 新图片为空则沿用旧文件名 else { $update_filename = $old_image; } $path = "../uploads"; $update_query = "UPDATE categories SET name='$name', slug='$slug', description='$description', meta_title='$meta_title', meta_description='$meta_description', meta_keywords='$meta_keywords', status='$status', popular='$popular', image='$update_filename' WHERE id= '$category_id' "; $update_query_run = mysqli_query($con, $update_query); if($update_query_run) { if($_FILES['image']['name'] != "") { move_uploaded_file($_FILES['image']['tmp_name'], $path.'/'.$update_filename); // 修复后删除旧文件逻辑 if(file_exists("../uploads/".$old_image)) { unlink("../uploads/".$old_image); } } redirect("edit-category.php?id=$category_id", "Category updated successfully!"); } else { redirect("edit-category.php?id=$category_id", "Update failed"); } }
内容的提问来源于stack exchange,提问作者mynameiswadey
相关产品推荐
相关产品推荐

