猿问

如何使用PHP删除文件夹中的多个上传图像

如何在 PHP 中删除多个图像。但单个产品删除工作正常。


if (isset($_REQUEST['delete_product'])) {

    $ca_in_id = $_REQUEST['delete_product'];

    $where = array("ca_in_id" => $ca_in_id);


    $data = $this->select_where('catalog_inventory', $where);

    //$res = $data->fetch_object();

    

    $result = $this->delete_where('catalog_inventory', $where);

    if ($result) {

        // (file_exists($pic1)) {

        //  print_r($pic1);

        // exit();

        while ($row = mysqli_fetch_array($data)) {

            $image = $row["upload_file"];

            $pic1 = ("$image");

            unlink('product_images/' . $pic1);

            print_r($pic1);


        }

        exit();

        //}

        ?>

        <script>

            alert("Delete Sucess");

            window.location = "list-product";

        </script>

    <?php

    } else {

    ?>

        <script>

            alert("Delete Fails");

            window.location = "list-product";

        </script>

    <?php

    }

}

多个图像删除显示此错误:-

如何解决这个问题?



子衿沉夜
浏览 143回答 1
1回答

ibeautiful

我有解决问题首先获取数据库中的所有值并使用explode函数使用count数据库列值并最终使用 for 循环。这是我的代码:-这段代码的意思是获取数据库中的值。&nbsp; $data = $this->select_where('catalog_inventory', $where);&nbsp; $res = $data->fetch_object();&nbsp; $image = $res->upload_file;&nbsp; $allimages = explode(",", $image);&nbsp; $countallimages = count($allimages);获得多个值和值后运行循环&nbsp;for ($i = 0; $i < $countallimages; $i++) {&nbsp; &nbsp; if (file_exists("product_images/" . $allimages[$i]) == false) {&nbsp; &nbsp; &nbsp; &nbsp; echo "error ";&nbsp; &nbsp; &nbsp; &nbsp; exit();&nbsp; &nbsp; }}for ($i = 0; $i < $countallimages; $i++) {&nbsp; &nbsp; $path = "product_images/" . $allimages[$i];&nbsp; &nbsp; if (empty($path)) {&nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; unlink($path);&nbsp; &nbsp; }}谢谢 ...
随时随地看视频慕课网APP
我要回答