如何在 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 }}多個圖像刪除顯示此錯誤:-如何解決這個問題?
1 回答

ibeautiful
TA貢獻1993條經驗 獲得超6個贊
我有解決問題首先獲取數據庫中的所有值并使用explode函數使用count數據庫列值并最終使用 for 循環。
這是我的代碼:-
這段代碼的意思是獲取數據庫中的值。
$data = $this->select_where('catalog_inventory', $where);
$res = $data->fetch_object();
$image = $res->upload_file;
$allimages = explode(",", $image);
$countallimages = count($allimages);
獲得多個值和值后運行循環
for ($i = 0; $i < $countallimages; $i++) {
if (file_exists("product_images/" . $allimages[$i]) == false) {
echo "error ";
exit();
}
}
for ($i = 0; $i < $countallimages; $i++) {
$path = "product_images/" . $allimages[$i];
if (empty($path)) {
} else {
unlink($path);
}
}
謝謝 ...
- 1 回答
- 0 關注
- 174 瀏覽
添加回答
舉報
0/150
提交
取消