| Server IP : 173.209.174.21 / Your IP : 216.73.216.89 Web Server : Apache/2.4.58 (Ubuntu) System : Linux wcfs-server 6.8.0-124-generic #124-Ubuntu SMP PREEMPT_DYNAMIC Tue May 26 13:00:45 UTC 2026 x86_64 User : nodor ( 1000) PHP Version : 8.3.6 Disable Function : NONE MySQL : OFF | cURL : ON | WGET : ON | Perl : ON | Python : OFF | Sudo : ON | Pkexec : OFF Directory : /var/www/wcfs/fuelmapping/ |
Upload File : |
<?php
// combine_columns.php
// Upload a CSV and rewrite Column C => "original C, A" (preserving C)
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_FILES['csv_file'])) {
$err = null;
if (!is_uploaded_file($_FILES['csv_file']['tmp_name'])) {
$err = "No file uploaded.";
} else {
$tmp = $_FILES['csv_file']['tmp_name'];
$name = basename($_FILES['csv_file']['name']);
$firstRowHeader = isset($_POST['header']) && $_POST['header'] === '1';
// Open input
$in = fopen($tmp, 'r');
if (!$in) $err = "Unable to open uploaded file.";
if (!$err) {
// Prepare an in-memory output stream
$out = fopen('php://temp', 'w+');
if (!$out) $err = "Unable to create output stream.";
if (!$err) {
$rowIndex = 0;
while (($row = fgetcsv($in)) !== false) {
// Optionally leave the header row untouched
if ($rowIndex === 0 && $firstRowHeader) {
fputcsv($out, $row);
$rowIndex++;
continue;
}
// Ensure indexes exist (A=0, C=2)
// Pad the row up to at least 3 columns
for ($i = count($row); $i < 3; $i++) $row[$i] = '';
$colA = isset($row[0]) ? trim((string)$row[0]) : '';
$colC = isset($row[2]) ? trim((string)$row[2]) : '';
// Rewrite Column C: keep C, and append ", " + A (when applicable)
if ($colC !== '' && $colA !== '') {
$row[2] = $colC . ', ' . $colA;
} elseif ($colC === '' && $colA !== '') {
$row[2] = $colA; // no leading comma if C was empty
} else {
// leave as-is if A is empty (or both empty)
$row[2] = $colC;
}
fputcsv($out, $row);
$rowIndex++;
}
// Ship the file back to the browser
rewind($out);
$downloadName = 'combined_' . preg_replace('/[^A-Za-z0-9._-]/', '_', $name);
header('Content-Type: text/csv');
header('Content-Disposition: attachment; filename="'.$downloadName.'"');
header('Cache-Control: no-store, no-cache, must-revalidate, max-age=0');
fpassthru($out);
fclose($out);
fclose($in);
exit;
}
}
}
if ($err) {
http_response_code(400);
echo "<!doctype html><html><body><p style='color:red;'>$err</p><p><a href='".$_SERVER['PHP_SELF']."'>Go back</a></p></body></html>";
exit;
}
}
?>
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>Combine CSV Columns (C = C, A)</title>
<meta name="viewport" content="width=device-width, initial-scale=1" />
<style>
body{font-family:system-ui,-apple-system,Segoe UI,Roboto,Arial,sans-serif;line-height:1.45;padding:2rem;background:#f7f7fb;color:#222}
.card{max-width:640px;margin:0 auto;background:#fff;border-radius:14px;box-shadow:0 8px 24px rgba(0,0,0,.08);padding:24px;}
h1{font-size:1.25rem;margin:0 0 1rem}
.hint{color:#555;margin:.5rem 0 1rem}
label{display:block;margin:.5rem 0 .25rem}
input[type="file"]{display:block;margin:.25rem 0 1rem}
.row{display:flex;align-items:center;gap:.5rem;margin:.25rem 0 1rem}
button{appearance:none;border:0;background:#111;color:#fff;padding:.75rem 1rem;border-radius:10px;cursor:pointer}
button:hover{background:#000}
small{color:#666}
</style>
</head>
<body>
<div class="card">
<h1>Combine Columns: C = (original C) + �, � + A</h1>
<p class="hint">Upload a CSV. For each data row, Column C will keep its original value and have Column A appended after a comma. If C is empty, it becomes A (no leading comma). Headers can be preserved.</p>
<form method="post" enctype="multipart/form-data">
<label for="csv_file">CSV file</label>
<input type="file" id="csv_file" name="csv_file" accept=".csv" required>
<div class="row">
<input type="checkbox" id="header" name="header" value="1" checked>
<label for="header" style="margin:0;">First row is a header</label>
</div>
<button type="submit">Process & Download</button>
<div style="margin-top:.75rem;">
<small>Notes: expects standard comma-separated CSV. Works with any number of columns; only A and C are modified.</small>
</div>
</form>
</div>
</body>
</html>