Matt Murphy Matt Murphy - 4 years ago 109
PHP Question

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
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

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);

print 'Success!';
die('Error : ('. $mysqli->errno .') '. $mysqli->error);
print 'The passwords do not match!';
print 'Enter all fields please';

Answer Source

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:

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download