-0.8 C
New York
Saturday, September 18, 2021
HomePHPPHP registration with file upload and validation

PHP registration with file upload and validation

We’re going to make a registration system with PHP and MySQL. It’ll also show you how to you can validate your profile image and upload in your server.

HTML Form

<form action="" method="POST" enctype="multipart/form-data">
    <img class="mb-4" src="assets/images/bootstrap-logo.svg" alt="" width="72" height="57">
    <h1 class="h3 mb-3 fw-normal">Please Register</h1>

    <?php if (isset($success)) { ?>
    <p class="alert alert-success">
        <?php echo $success; ?>
    </p>
    <?php } ?>
    <?php if (isset($errors)) {
                foreach ($errors as $error) { ?>
    <p class="alert alert-warning">
        <?php echo $error; ?>
    </p>
    <?php }
            } ?>

    <label for="inputUsername" class="visually-hidden">Username</label>
    <input type="text" name="username" id="inputUsername" class="form-control" placeholder="Username">

    <label for="inputEmail" class="visually-hidden">Email address</label>
    <input type="email" name="email" id="inputEmail" class="form-control" placeholder="Email address">

    <label for="myFile" class="visually-hidden">Image</label>
    <input type="file" name="file" id="myFile" class="form-control">

    <label for="inputPassword" class="visually-hidden">Password</label>
    <input type="password" name="password" id="inputPassword" class="form-control" placeholder="Password">

    <button class="w-100 btn btn-lg btn-primary" type="submit" name="register">Register</button>

    <p class="mt-5 mb-3 text-muted">&copy; 2017-2020</p>
</form>

PHP Code

 if (isset($_POST['register'])) {
            $username       = trim($_POST['username']);
            $email          = trim($_POST['email']);
            $password       = $_POST['password'];
            $profile_photo  = $_FILES['file'];

            // validation
            if (empty($username)) {
                $errors[] = "You must enter a valid username";
            }
            if (empty($email)) {
                $errors[] = "You must enter a valid email";
            }
            if (filter_var($email, FILTER_VALIDATE_EMAIL) === false) {
                $errors[] = "You must enter a valid email";
            }
            if (empty($profile_photo['name'])) {
                $errors[] = "You must upload a valid file";
            }
            if (strlen($password) < 6) {
                $errors[] = "You must enter minimum 6 chars password";
            }
            $password = password_hash($_POST['password'], PASSWORD_BCRYPT);

            if (empty($errors)) {
                // mysql connection
                $link = mysqli_connect("localhost", "root", "", "php7practice");

                if ($link === false) {
                    echo "Error: Unable to connect to MySQL." . PHP_EOL;
                    echo "Debugging errno: " . mysqli_connect_errno($link) . PHP_EOL;
                    echo "Debugging error: " . mysqli_connect_error($link) . PHP_EOL;
                    echo "Host information: " . mysqli_get_host_info($link) . PHP_EOL;
                    exit;
                }

                // file upload
                if ($profile_photo['size'] > 1000000) {
                    $errors[] = "File size exceeded";
                }
                $file_data = explode('.', $profile_photo['name']);
                $file_ext = end($file_data); // 05 number class - 00:35:00
                if (!in_array($file_ext, ['jpg', 'png', 'jpeg'], true)) {
                    $errors[] = "Upload a valid image";
                }
                $new_file_name = uniqid('pp_', true) . '.' . $file_ext;
                $upload = move_uploaded_file($profile_photo['tmp_name'], 'assets/images/profile_photo/' . $new_file_name);

                if ($upload) {
                    # db insert
                    $query = "INSERT INTO users ( username, email, profile_photo, password ) VALUES ( '$username', '$email', '$new_file_name', '$password' )";
                    $insert = mysqli_query($link, $query);
                    if ($insert) {
                        $success = "Data inserted successfully";
                    } else {
                        echo mysqli_error($link);
                    }
                } else {
                    $errors[] = "File was not uploaded";
                }
            }
        }

SQL Code

CREATE TABLE `users` (
	`id` BIGINT(20) NOT NULL AUTO_INCREMENT,
	`username` VARCHAR(64) NOT NULL,
	`email` VARCHAR(64) NOT NULL,
	`password` VARCHAR(64) NOT NULL,
	`profile_photo` VARCHAR(125) NOT NULL,
	PRIMARY KEY (`id`),
	UNIQUE INDEX `username` (`username`),
	UNIQUE INDEX `email` (`email`),
	INDEX `id` (`id`)
)
COLLATE='latin1_swedish_ci'
ENGINE=InnoDB
AUTO_INCREMENT=20
;

5 COMMENTS

  1. Great blog right here! Additionally your web site so much up very fast! What host are you the use of? Can I get your associate link to your host? I want my site loaded up as quickly as yours lol Guinna Giuseppe Stephan

Leave a Reply

Most Popular

Recent Comments

%d bloggers like this: