FAUSTINE MWOYA November 20, 2025

Jinsi ya Kuunda File Upload System Kwa Dashboard Yenye Login Authentication

Jifunze jinsi ya kuunda secure file upload system kwa PHP ambayo inahitaji login ya dashboard. Mtumiaji lazima aingie kwanza kwenye dashboard, kisha aweze kupakia file kwa usalama. Tutorial hii ni fupi lakini yenye code working example.

Example Code:
1. Login Page (login.php)
<?php
session_start();
$admin_user = "admin";
$admin_pass = "password123";
$error = "";

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$user = $_POST['username'] ?? '';
$pass = $_POST['password'] ?? '';

if ($user === $admin_user && $pass === $admin_pass) {
$_SESSION['admin_logged'] = true;
header("Location: dashboard.php");
exit;
} else {
$error = "<div class='alert alert-danger text-center'>Username au password sio sahihi!</div>";
}
}

if (isset($_SESSION['admin_logged']) && $_SESSION['admin_logged'] === true) {
header("Location: dashboard.php");
exit;
}
?>
<form method="POST">
<?= $error ?>
<input type="text" name="username" placeholder="Username" required>
<input type="password" name="password" placeholder="Password" required>
<button type="submit">Login</button>
</form>

2. Dashboard with File Upload (dashboard.php)
<?php
session_start();
if (!isset($_SESSION['admin_logged']) || $_SESSION['admin_logged'] !== true) {
header("Location: login.php");
exit;
}

$upload_error = "";
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_FILES['file'])) {
$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["file"]["name"]);

if (move_uploaded_file($_FILES["file"]["tmp_name"], $target_file)) {
$upload_error = "<div class='alert alert-success'>File imepakiwa kwa ufanisi!</div>";
} else {
$upload_error = "<div class='alert alert-danger'>Kuna tatizo katika kupakia file.</div>";
}
}
?>
<h1>Dashboard</h1>
<a href="logout.php">Logout</a>

<h2>Upload File</h2>
<?= $upload_error ?>
<form method="POST" enctype="multipart/form-data">
<input type="file" name="file" required>
<button type="submit">Upload</button>
</form>

3. Logout (logout.php)
<?php
session_start();
session_unset();
session_destroy();
header("Location: login.php");
exit;
?>

Links Za Kujifunza Zaidi

🌐 Faulink Official Website:
https://www.faulink.com/

📘 Jifunze Web Design & Programming:
https://www.faulink.com/excel_mifumo.php

📲 Piga / WhatsApp kwa msaada wa haraka:
https://wa.me/255693118509
Share this post
Previous Next

Comments

0
No comments yet. Be the first to comment.

Continue Reading

Subscribe

Get new updates

Jiunge upokee posts mpya, tutorials, na updates za mifumo moja kwa moja kwenye email yako.

Chat na Faulink