Like Us Like Us Facebook Subscribe Subscribe us YouTube Whatsapp Share us Whatsapp Query Send your queries

Upload user profile image and save to data base -PHP MYSQLI

Upload user profile image and save to data base -PHP MYSQLI

 If you are working on customized CMS or Social networking website then user profile image may be or may not be a head ache I have developed my own where we can upload and rename the image username.jpg/png/gif with size restriction to 200KB.

PHP MYSQLI Upload Image Tutorial

First design a table where database name is Sanjay.
Create a table userImage  fileds
user – username will be stored
url – url of img stored
lastUpload- when the upload was done.
Create a folder upload/   where  all images will stored.

[php]
<?php
$user=”sanjay”; //you can fetch username here
$db=new mysqli(‘localhost’,’root’,”,’Sanjay’);
if($db->connect_errno){
echo $db->connect_error;
}
$pull=”select * from userImage where user=’$user'”;
$allowedExts = array(“jpg”, “jpeg”, “gif”, “png”,”JPG”);
$extension = @end(explode(“.”, $_FILES[“file”][“name”]));
if(isset($_POST[‘pupload’])){
if ((($_FILES[“file”][“type”] == “image/gif”)
|| ($_FILES[“file”][“type”] == “image/jpeg”)
|| ($_FILES[“file”][“type”] == “image/JPG”)
|| ($_FILES[“file”][“type”] == “image/png”)
|| ($_FILES[“file”][“type”] == “image/pjpeg”))
&& ($_FILES[“file”][“size”] < 200000)
&& in_array($extension, $allowedExts))
{
if ($_FILES[“file”][“error”] > 0)
{
echo “Return Code: ” . $_FILES[“file”][“error”] . “<br>”;
}
else
{
echo ‘<div class=”plus”>’;
echo “Uploaded Successully”;
echo ‘</div>’;echo”<br/><b><u>Image Details</u></b><br/>”;

echo “Name: ” . $_FILES[“file”][“name”] . “<br/>”;
echo “Type: ” . $_FILES[“file”][“type”] . “<br/>”;
echo “Size: ” . ceil(($_FILES[“file”][“size”] / 1024)) . ” KB”;

if (file_exists(“upload/” . $_FILES[“file”][“name”]))
{
unlink(“upload/” . $_FILES[“file”][“name”]);
}
else{
$pic=$_FILES[“file”][“name”];
$conv=explode(“.”,$pic);
$ext=$conv[‘1′];
move_uploaded_file($_FILES[“file”][“tmp_name”],”upload/”. $user.”.”.$ext);
echo “Stored in as: ” . “upload/”.$user.”.”.$ext;
$url=$user.”.”.$ext;

$query=”update userImage set url=’$url’, lastUpload=now() where user=’$user'”;
if($upl=$db->query($query)){
echo “<br/>Saved to Database successfully”;
}
}
}
}else{
echo “File Size Limit Crossed 200 KB Use Picture Size less than 200 KB”;
}
}
?>
<form action=”” method=”post” enctype=”multipart/form-data”>
<?php
$res=$db->query($pull);
$pics=$res->fetch_assoc();
echo ‘<div class=”imgLow”>’;
echo “<img src=’upload/$pics[url]’ alt=’profile picture’ width=’80 height=’64’ class=’doubleborder’/></div>”;
?>
<input type=”file” name=”file” />
<input type=”submit” name=”pupload” class=”button” value=”Upload”/>
</form>
[/php]

Hope You will understand this simple script.

Tutorial is update on 31 May 2017, working fine tested on  XAMPP Version 7.0.8

if you want complete script with foundation framework, profile mangement, password change, user profile picture management then download my open -source project Adminplus at githhub
download Zip file extract and save it to htdocs folder , if using Linux set permission . open phpmyadmin and create a database sanjay_plus now import  sql . Now we can login using username sanjay password openplus.in.

Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

24 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
oseh felix

i need the database of this script

mkmobi

i also need profile management script with photo upload option
can you help me

cherese

what are the rows inside your sanjay? does it includes the username and password?

Cik Cactus

How to show the image in user profile?

RAJENDRA YADAV

if u want to show user profile image i can tell u contact me, , 8003555643

Dsk Dayarathne

thanks

Fully Help

Very clear and helpful for beginners. Great job you did here! A html tutorials that covers all the basics of html with examples and helps to learn html for Beginners Online.

Anonymous

Hi there
i just want to say a million thanks 4 your script.
god bless you

shashi ranjan

how to upload a image in tabular format

Anonymous

sir the image upload success mesg is displayed but image not change what is the problem..

King Pin

please i want the codes to create users profile picture

King Pin

please i want the codes for users to upload thier profile picture

asad

Your CODE IS NOT WORKING giving ERRORR on

Notice: Undefined variable: _SESSION in C:\xampp\htdocs\xampp\PROJECT\dash.php on line 354

Fatal error: Uncaught Error: Call to a member function fetch_assoc() on boolean in C:\xampp\htdocs\xampp\PROJECT\dash.php:420 Stack trace: #0 {main} thrown in C:\xampp\htdocs\xampp\PROJECT\dash.php on line 420

Exact code Ctrl+C andCtrl+V

Jimmy

Is this thread still active as well as this script? I am in need of some help I am getting the following error:

Fatal error: Call to a member function fetch_assoc() on boolean in C:\wamp64\www\2\profileimg.php on line 59

Cristel Castillo

how can i display the picture? Please tell me for my project. Thanks

Do you have any Query / Suggestion