Matt Murphy
Matt Murphy

Reputation: 265

OO Profile picture upload through PHP

I have developed a registration form using object-oriented techniques, it works fine except when I try and incorportate a file upload so that the user can have a picture saved in the database.

In my database I am using the BLOB format and using file_get_contents to retrieve the file uploaded.

I previously had $profilepic = $_POST['photo']; which is why $_POST['photo'] is still in the second if statement.

I'm a little confused what format to use as I've not done a lot of object oriented.

Thanks in advance

<center>
<?php
session_start();
include 'registrationform.php';
include 'connection.php';

if (isset($_POST['regsubmit'])) 
    {
    $firstname = $_POST['firstname'];
    $firstname = ucfirst($firstname);
    $lastname = $_POST['lastname'];
    $lastname = ucfirst($lastname);
    $user = $_POST['username'];
    $user = ucfirst($user);
    $pass = $_POST['password'];
    $spass = $_POST['secondpassword'];
    $profilepic = file_get_contents($_FILES['photo']['tmp_name']);

    if($_POST['firstname'] && $_POST['lastname'] && $_POST['photo'] && $_POST['username'] && $_POST['password'] && $_POST['secondpassword'])
    {
    if ($spass == $pass)
    {
    $query = "INSERT INTO users (firstname, lastname, photo, username, password) VALUES(?, ?, ?, ?, ?)";
    $statement = $connection->prepare($query);
    $statement->bind_param('ssbss', $firstname, $lastname, $profilepic, $user, $pass);

    if($statement->execute()){
        print 'Success!'; 
        }else  
        {
        die('Error : ('. $mysqli->errno .') '. $mysqli->error);
        }
        $statement->close();
        }
        else
        {
           print 'The passwords do not match!';
        }
    }
    else
    {
        print 'Enter all fields please';
    }
    }
?>
</center>

Upvotes: 2

Views: 289

Answers (1)

user5570620
user5570620

Reputation:

You can move uploaded image in folder using move_uploaded_file() and store image name in database.
You can get image name as following:

$image_name = $_FILES["photo"]["name"];

For showing image you can retrieve image name.
Then in html you can show image like following:
<img src="YOUR_IMAGE_FOLDER_PATH/".$IMAGE_NAME>

Upvotes: 2

Related Questions