| Server IP : 103.243.232.44 / Your IP : 216.73.216.69 Web Server : LiteSpeed System : Linux server17213-10344.hostycare.online 5.14.0-687.38.1.el9_8.x86_64 #1 SMP PREEMPT_DYNAMIC Wed Aug 12 17:19:12 EDT 2026 x86_64 User : iamakash ( 1400) PHP Version : 8.1.34 Disable Function : NONE MySQL : OFF | cURL : ON | WGET : ON | Perl : ON | Python : OFF | Sudo : OFF | Pkexec : OFF Directory : /home/iamakash/.trash/ |
Upload File : |
<?php
require __DIR__ . '/vendor/autoload.php';
require __DIR__ . '/includes/db.php';
// Parsing ~145k rows via PhpSpreadsheet can take ~30s + RAM.
// upload_max_filesize, post_max_size, max_execution_time, memory_limit
// are set in .htaccess (ini_set can't change upload/post limits at runtime).
set_time_limit(300);
ini_set('memory_limit', '512M');
$errors = [];
$summary = null;
$uploadRow = null;
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$baseYear = trim($_POST['base_year'] ?? '2011-12');
if (!isset($_FILES['file']) || $_FILES['file']['error'] !== UPLOAD_ERR_OK) {
$errors[] = 'Please choose a file to upload. (Error code: '
. ($_FILES['file']['error'] ?? 'no-file') . ')';
} else {
$origName = $_FILES['file']['name'];
$tmp = $_FILES['file']['tmp_name'];
$size = (int)$_FILES['file']['size'];
$ext = strtolower(pathinfo($origName, PATHINFO_EXTENSION));
if (!in_array($ext, ['xls', 'xlsx'], true)) {
$errors[] = 'File must be an .xls or .xlsx Excel file.';
}
if (!$errors) {
$uploadsDir = __DIR__ . '/uploads';
if (!is_dir($uploadsDir)) {
mkdir($uploadsDir, 0775, true);
}
$safeBase = preg_replace('/[^A-Za-z0-9._-]+/', '_', pathinfo($origName, PATHINFO_FILENAME));
$stored = date('Ymd_His') . '_' . $safeBase . '.' . $ext;
$storedPath = $uploadsDir . DIRECTORY_SEPARATOR . $stored;
if (!move_uploaded_file($tmp, $storedPath)) {
$errors[] = 'Failed to save the uploaded file.';
} else {
$pdo = getPDO();
// Create an upload log row (status=processing)
$ins = $pdo->prepare(
'INSERT INTO uploads
(filename, stored_path, size_bytes, base_year, status, notes)
VALUES (:f, :p, :s, :b, :st, :n)'
);
$ins->execute([
':f' => $origName,
':p' => $storedPath,
':s' => $size,
':b' => $baseYear,
':st' => 'processing',
':n' => null,
]);
$uploadId = (int)$pdo->lastInsertId();
try {
$importer = new Shaurya\ExcelImporter($pdo, $baseYear);
$summary = $importer->import($storedPath, $uploadId);
$upd = $pdo->prepare(
'UPDATE uploads SET
commodities_seen = :c,
periods_seen = :p,
values_written = :v,
latest_period = :lp,
status = :st,
notes = :n
WHERE id = :id'
);
$upd->execute([
':c' => $summary['commodities_seen'],
':p' => $summary['periods_seen'],
':v' => $summary['values_written'],
':lp' => $summary['latest_period'],
':st' => 'completed',
':n' => 'Sheet: ' . $summary['sheet'],
':id' => $uploadId,
]);
$uploadRow = ['id' => $uploadId, 'filename' => $origName];
} catch (\Throwable $e) {
$upd = $pdo->prepare(
'UPDATE uploads SET status=:st, notes=:n WHERE id=:id'
);
$upd->execute([
':st' => 'failed',
':n' => $e->getMessage(),
':id' => $uploadId,
]);
$errors[] = 'Import failed: ' . $e->getMessage();
}
}
}
}
}
$pageTitle = 'Upload — Shaurya Index Factor';
require __DIR__ . '/includes/header.php';
?>
<div class="row g-4">
<div class="col-lg-7">
<div class="card">
<div class="card-body">
<h4 class="mb-3">Upload WPI Monthly Index File</h4>
<p class="text-muted small">
Download the consolidated file from
<a href="https://eaindustry.nic.in/download_data_1112.asp" target="_blank" rel="noopener">
eaindustry.nic.in · WPI Download Data (2011‑12 Series)
</a>
— under "Monthly Index Files" click
<em>From APR-2012 Onwards</em>. Upload the resulting
<code>monthly_index_YYYYMM.xls</code> file here.
</p>
<?php if ($errors): ?>
<div class="alert alert-danger">
<?php foreach ($errors as $e): ?>
<div><?= h($e) ?></div>
<?php endforeach; ?>
</div>
<?php endif; ?>
<?php if ($summary): ?>
<div class="alert alert-success">
<strong>Import successful.</strong>
<ul class="mb-0 mt-2">
<li>Sheet: <code><?= h($summary['sheet']) ?></code></li>
<li>Commodities upserted: <strong><?= (int)$summary['commodities_seen'] ?></strong></li>
<li>Months (periods) in file: <strong><?= (int)$summary['periods_seen'] ?></strong></li>
<li>Index values upserted: <strong><?= number_format((int)$summary['values_written']) ?></strong></li>
<li>Latest period: <code><?= h($summary['latest_period']) ?></code> <span class="text-muted">(last 2 months marked provisional)</span></li>
</ul>
<a href="<?= base_url('browse.php') ?>" class="btn btn-sm btn-primary mt-2">Browse data</a>
</div>
<?php endif; ?>
<form method="post" enctype="multipart/form-data" class="mt-3">
<div class="mb-3">
<label for="file" class="form-label">Excel file (.xls / .xlsx)</label>
<input type="file" name="file" id="file" accept=".xls,.xlsx" class="form-control" required>
<div class="form-text">Max 50 MB. Parsing takes ~30 seconds — please wait.</div>
</div>
<div class="mb-3">
<label for="base_year" class="form-label">Base Year</label>
<select name="base_year" id="base_year" class="form-select" style="max-width: 180px;">
<option value="2011-12" selected>2011-12 (current)</option>
<option value="2004-05">2004-05 (legacy)</option>
</select>
</div>
<button type="submit" class="btn btn-primary">Upload & Import</button>
</form>
</div>
</div>
</div>
<div class="col-lg-5">
<div class="card">
<div class="card-body">
<h5>Recent uploads</h5>
<?php
$rows = getPDO()->query(
'SELECT id, filename, status, commodities_seen, values_written,
latest_period, uploaded_at
FROM uploads ORDER BY id DESC LIMIT 10'
)->fetchAll();
?>
<?php if (!$rows): ?>
<p class="text-muted small mb-0">No uploads yet.</p>
<?php else: ?>
<div class="table-responsive">
<table class="table table-sm">
<thead>
<tr>
<th>#</th>
<th>File</th>
<th>Status</th>
<th class="text-end">Rows</th>
<th>Latest</th>
</tr>
</thead>
<tbody>
<?php foreach ($rows as $r): ?>
<tr>
<td><?= (int)$r['id'] ?></td>
<td class="small"><?= h($r['filename']) ?><br>
<span class="text-muted"><?= h($r['uploaded_at']) ?></span></td>
<td>
<?php
$badge = ['completed'=>'success','processing'=>'warning','failed'=>'danger','pending'=>'secondary'];
$cls = $badge[$r['status']] ?? 'secondary';
?>
<span class="badge bg-<?= $cls ?>"><?= h($r['status']) ?></span>
</td>
<td class="text-end small"><?= number_format((int)$r['values_written']) ?></td>
<td class="small"><code><?= h($r['latest_period']) ?></code></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</div>
<?php endif; ?>
</div>
</div>
</div>
</div>
<?php require __DIR__ . '/includes/footer.php'; ?>