db->transStart(); $builder = $this->db->table('ip_invoice_attachment'); $builder->insert($attachments); $insert_id = $this->db->affectedRows(); $this->db->transComplete(); return $insert_id; } // Ipinvoice_model public function deleteAttachment($invoice_attachment_id) { // Get the attachment details to get the file path $builder = $this->db->table('ip_invoice_attachment'); $builder->where('invoice_attachment_id', $invoice_attachment_id); $query = $builder->get(); $attachment = $query->getRowArray(); // Get single row as an associative array if ($attachment) { // Construct the file path $fileName = $attachment['attachment_name']; // Make sure this is the correct field name $filePath = './public/uploads/images/invoice_files/' . $fileName; // Debug: Log the file path log_message('error', 'Attempting to delete file: ' . $filePath); // Check if the path is actually a file if (file_exists($filePath) && !is_dir($filePath)) { unlink($filePath); // Delete the file } else { log_message('error', 'File does not exist or is a directory: ' . $filePath); } // Delete the record from the database $builder->where('invoice_attachment_id', $invoice_attachment_id); $builder->delete(); return $this->db->affectedRows() > 0; } return false; } }