transBegin(); $query = $db->connect()->query("SELECT * FROM customer_upload_ltms WHERE id=".$id); if($query->num_rows ==1) { $is_valid = 1; $is_active =1; $created_by = $_SESSION['user']['id']; $created_at = date('Y-m-d h:i:s'); foreach($query as $row){ $file_name = $row['filename']; $source_path = '../../dist/img/customer/ltms-customer-upload/'.$file_name; // var_dump($file_name ); $destination_path = '../../dist/img/customer/ltms/'.$file_name;; // Replace with actual destination path if (copy($source_path, $destination_path)) { // Successfully copied the file, update the database $db->connect()->query("UPDATE customer_upload_ltms SET status = 2 WHERE id=" . intval($id)); $insert = $db->connect()->query("INSERT INTO `customer_ltms`(`file`,`customer_id`,`created_at`, `is_valid`,`is_active`,`created_by`) VALUES ('" . $file_name. "','" . $customer_id . "' ,'" . $created_at . "','".$is_valid."','".$is_valid ."','1')"); $db->transCommit(); // Commit the transaction echo json_encode(['status' => 1, 'message' => "File attached successfully."], JSON_THROW_ON_ERROR); } else { // Failed to copy the file, rollback the transaction $db->transRollback(); echo json_encode(['status' => 0, 'message' => "Failed to attach file."], JSON_THROW_ON_ERROR); } } } } else { return 'no data found.'; } } ?>