Wednesday, March 20, 2013

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.
$db=new mysqli('localhost','root','','Sanjay');
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 ((($_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>";
    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"]);

      "upload/". $user.".".$ext);
      echo "Stored in as: " . "upload/" . $user.".".$ext;
      $query="update userImage set url='$url', lastUpload=now() where user='$user'";
          echo "<br/>Saved to Database successfully";
  echo "File Size Limit Crossed 200 KB Use Picture Size less than 200 KB";

<form action="" method="post" enctype="multipart/form-data">   
    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"/>

Hope You will understand this simple script.

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

Delicious Save this on Delicious


  1. i need the database of this script

  2. I am ready to give you db for this script
    contact me @ sonzoy[@], if you want to see this script live demo please register my social techno site open plus

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

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

    1. rows.. ok
      username and img, or you want extend it then create more

  4. How to show the image in user profile?

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

    1. A complete login system with password change, use of profile image secure is going to publish on my new website
      Silent feature
      Responsive using framework foundation
      profile mangement
      password hashing using md5 and sha256
      easy to use interface

      and I will provide complete package under project name adminplus which will be opensource please allow me sometime and register on my website to check launch deadline

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

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

  8. how to upload a image in tabular format


If you have any Question , Suggestion or idea then please comment regarding the post.