diff --git a/SeedDMS_Core/Core/inc.ClassDocument.php b/SeedDMS_Core/Core/inc.ClassDocument.php index 2a89f7185..ba9f9bec8 100644 --- a/SeedDMS_Core/Core/inc.ClassDocument.php +++ b/SeedDMS_Core/Core/inc.ClassDocument.php @@ -2358,29 +2358,37 @@ class SeedDMS_Core_Document extends SeedDMS_Core_Object { /* {{{ */ } $db->commitTransaction(); + unset ($this->_documentFiles); return $file; } /* }}} */ function removeDocumentFile($ID) { /* {{{ */ $db = $this->_dms->getDB(); - if (!is_numeric($ID)) return false; + if (!is_numeric($ID) || $ID < 1) + return false; $file = $this->getDocumentFile($ID); if (is_bool($file) && !$file) return false; - if (file_exists( $this->_dms->contentDir . $file->getPath() )){ - if (!SeedDMS_Core_File::removeFile( $this->_dms->contentDir . $file->getPath() )) - return false; + $db->startTransaction(); + /* First delete the database record, because that can be undone + * if deletion of the file fails. + */ + $queryStr = "DELETE FROM `tblDocumentFiles` WHERE `document` = " . $this->getID() . " AND `id` = " . (int) $ID; + if (!$db->getResult($queryStr)) { + $db->rollbackTransaction(); + return false; } - $name=$file->getName(); - $comment=$file->getcomment(); - - $queryStr = "DELETE FROM `tblDocumentFiles` WHERE `document` = " . $this->getID() . " AND `id` = " . (int) $ID; - if (!$db->getResult($queryStr)) - return false; + if (file_exists( $this->_dms->contentDir . $file->getPath() )){ + if (!SeedDMS_Core_File::removeFile( $this->_dms->contentDir . $file->getPath() )) { + $db->rollbackTransaction(); + return false; + } + } + $db->commitTransaction(); unset ($this->_documentFiles); return true;