'WFS', 'version' => '1.1.0', 'request' => 'GetFeature', 'typeName' => $layerName, 'outputFormat' => 'application/json', 'srsName' => 'EPSG:4326' ]; // Add CQL filter if primary key is specified if ($pkcol && $pkval !== null && $pkval !== '') { $params['CQL_FILTER'] = "$pkcol='$pkval'"; } // Add max features limit if ($maxFeatures !== null) { $params['maxFeatures'] = $maxFeatures; } $url = $baseUrl . '/wfs?' . http_build_query($params); // Create context with authentication $context = stream_context_create([ 'http' => [ 'header' => "Authorization: Basic " . base64_encode($config['geoserver_username'] . ":" . $config['geoserver_password']), 'ignore_errors' => true ] ]); $response = @file_get_contents($url, false, $context); if ($response === false) { throw new Exception("Failed to fetch features from GeoServer"); } $data = json_decode($response, true); if (!isset($data['features'])) { throw new Exception("Invalid response from GeoServer WFS"); } return $data['features']; } // --- Fetch data from GeoServer --- try { $maxFeatures = $allRows ? null : 100; // Apply filter only if not showing all rows $filterPkcol = ($pkcol && !$allRows) ? $pkcol : null; $filterPkval = ($pkRaw !== null && $pkRaw !== '' && !$allRows) ? $pkRaw : null; $features = fetchFeaturesFromGeoServer($layerRaw, $filterPkcol, $filterPkval, $maxFeatures); // Extract properties into rows array $rows = []; foreach ($features as $feature) { if (isset($feature['properties'])) { $rows[] = $feature['properties']; } } // Collect columns $cols = []; if (!empty($rows)) { $cols = array_keys($rows[0]); } } catch (Throwable $e) { http_response_code(500); die('Failed to fetch data from GeoServer: ' . htmlspecialchars($e->getMessage())); } // --- CSV export --- if (isset($_GET['export']) && $_GET['export'] === 'csv') { try { // Fetch fresh data for CSV export $csvMaxFeatures = $allRows ? null : 100; $csvFilterPkcol = ($pkcol && !$allRows) ? $pkcol : null; $csvFilterPkval = ($pkRaw !== null && $pkRaw !== '' && !$allRows) ? $pkRaw : null; $csvFeatures = fetchFeaturesFromGeoServer($layerRaw, $csvFilterPkcol, $csvFilterPkval, $csvMaxFeatures); $csvRows = []; foreach ($csvFeatures as $feature) { if (isset($feature['properties'])) { $csvRows[] = $feature['properties']; } } $filename = preg_replace('/[^a-zA-Z0-9_.-]/', '_', $layerRaw); $filename .= $allRows ? "_all" : "_view"; $filename .= "_" . date('Ymd_His') . ".csv"; header('Content-Type: text/csv; charset=UTF-8'); header('Content-Disposition: attachment; filename="' . $filename . '"'); header('Pragma: no-cache'); header('Expires: 0'); $out = fopen('php://output', 'w'); if (!empty($csvRows)) { // Emit header row fputcsv($out, array_keys($csvRows[0])); // Emit data rows foreach ($csvRows as $row) { fputcsv($out, array_map(function($v){ if (is_bool($v)) return $v ? 'true' : 'false'; if (is_scalar($v) || $v === null) return $v; return json_encode($v); }, array_values($row))); } } fclose($out); exit; } catch (Throwable $e) { http_response_code(500); die('CSV export failed: ' . htmlspecialchars($e->getMessage())); } } ?> Layer Table – <?php echo htmlspecialchars($layerRaw); ?>

Layer:

$layerRaw]); $qsFiltered = http_build_query(array_filter([ 'layer' => $layerRaw, 'pkcol' => $pkcol ?: null, 'pk' => ($pkRaw !== null && $pkRaw !== '') ? $pkRaw : null ])); $qsAll = http_build_query(['layer' => $layerRaw, 'all' => 1]); ?> View All Rows Export CSV (this view) Export CSV (all rows)

Showing all rows (up to GeoServer limit) Filtered by = Showing all rows (up to GeoServer limit) Showing up to 100 rows
Displaying record(s) from GeoServer layer:

No rows.