Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

shahr's avatar
Level 10

How to upload an image and save the database in PHP MVC?

create.php

<form action="/store" method="post">
	<input type="text" name="name" placeholder="name">
	<input type="text" name="email" placeholder="email">
	<input type="file" name="image" placeholder="image">
	<button type="submit">Save</button>
</form>

route.php

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    if ($_SERVER['REQUEST_URI'] === '/store') {
        $controller->store();
    }
}

Controller.php

public function store() {
    $name = $_POST['name'];
    $name = $_POST['name'];
    $image = $_POST['image'];
    $this->user->create($name, $email, $image);
    header('Location: /');
}

User.php

class User {
	private $db;

	public function __construct() {
		$this->db = new PDO('mysql:host=localhost;dbname=php', 'root', '');
	}

	public function getAll() {
		$stmt = $this->db->query('SELECT * FROM users');
		return $stmt->fetchAll(PDO::FETCH_ASSOC);
	}

	public function create($name, $email) {
		$stmt = $this->db->prepare('INSERT INTO users (name, email, image) VALUES (:name, :email, :image)');
		$stmt->execute(['name' => $name, 'email' => $email, 'image' => $image]);
	}
0 likes
1 reply

Please or to participate in this conversation.