简体   繁体   中英

unlink doesn't seem to work in a foreach loop

So I have the following piece of code so that when I delete a row from the database I also delete the files associated with it, the code runs smoothly and I'm actually able to delete the rows from the database but somehow I'm unable to delete the files from the server directory, note that row "photo_filename" contains a name such as "photo.png" or so, also calling _ DIR _ from the file returns a path like this " ... \\Desktop\\project/procedures", I'm not even getting any warnings I tried echoing aa string if unlink was successful and some other string if not successful, but the weird thing is I don't get any output, it is as if the loop doesn't even run, can someone point me towards the right direction on what I'm missing right here. Thank you

try {
  $db->beginTransaction();    // Begin transaction
  $query = "DELETE FROM properties "
    . " WHERE property_id = :property_id";  // Delete requested property.
  $stmt = $db->prepare($query);
  $stmt->bindParam(":property_id", $property["property_id"], PDO::PARAM_INT);
  $stmt->execute();
  $query = "SELECT * FROM photos "
    . " WHERE property_id = :property_id";
  $stmt = $db->prepare($query);
  $stmt->bindParam(":property_id", $property["property_id"], PDO::PARAM_INT);
  $stmt->execute();
  foreach ($stmt->fetchAll(PDO::FETCH_ASSOC) as $photo) {
    try {
      unlink(__DIR__ . "/../img/" . $photo["image_filename"])
    } catch (Exception $e) {
      throw $e;
    }
  }
  $query = "DELETE FROM photos "
    . " WHERE property_id = :property_id";
  $stmt = $db->prepare($query);
  $stmt->bindParam(":property_id", $property["property_id"], PDO::PARAM_INT);
  $stmt->execute();
  $db->commit();
} catch (Exception $e) {    // If there is a problem
  $db->rollBack();  //If there was a problem undo the whole attempt to insert
  $session->getFlashBag()->add("error", "Hubo un problema" . $e->getMessage()); // Display a message
  redirect("/show.php?id=".$property_id); // And redirect
  exit;
}

我刚刚意识到自己的愚蠢错误,因此将“属性”表链接到“照片”表,因此当我开始交易并从表中删除该属性时,与之关联的照片也会自动删除,因此当我选择并循环浏览照片表以获取没有的文件,将其保留在此处,以防有人碰到类似的东西,谢谢大家的回应。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM