ahkeat
ahkeat

Reputation: 71

How to upload an image in PHP?

I try to upload image but it unsuccessful! All information will insert into database except image. I try use text and blob type for image before also not success. I only can use mysql because all of my coding using mysql to insert into database and not mysqli.

Below is my register code

<?php
//register
error_reporting(0);
include 'connect.php';
$submit = $_POST['register'];
//form data
$fullname = strip_tags($_POST['fullname']);
$username = strtolower(strip_tags($_POST['uname']));
$password = strip_tags($_POST['pass']);
$repeatpassword = strip_tags($_POST['r_pass']);
$useremail = strip_tags($_POST['useremail']);
$gender = strip_tags($_POST['gender']);
$date = date("Y-m-d");

if(isset($submit))
{
if ($fullname&&$username&&$password&&$repeatpassword&&$useremail&&$gender)
{
//open database
$connect = mysql_connect("localhost","root","");
mysql_select_db("phpforum"); //select database  

$namecheck = mysql_query("SELECT username FROM users WHERE username='$username'");
$count = mysql_num_rows($namecheck);
//check username in database
if($count >0)
{
$unameErr =  "*Username already taken!";
}else{
//check letter username
if (!preg_match("/^[a-zA-Z _]*$/",$username)) {
$unamelErr = "* Only letters and white space allowed"; 
}else{
//check match password and repeat password
if($password==$repeatpassword)
{
//check char length of username 
    if (strlen ($username)>20||strlen($username)< 3 || strlen ($fullname)>20||strlen($fullname)<3)
    {
     $lengthErr ="* Must Between 3 and 20 characters!";
    }else{
            //check password length
            if (strlen($password)>25||strlen($password)<6)
            {
                $lengthpErr = "* Must Between 6 and 25 characters!";
            } 
            else
            {
            if(!move_uploaded_file(($_FILES['image']['tmp_name'],"images/".$_FILES['image']['name']))){
                echo "Image upload fail!";
            }else{
            //register user
            $password = md5($password);
            $repeatpassword = md5($repeatpassword);

            $queryreg = mysql_query("INSERT INTO users (id,name,username,password,email,date,level,image,gender)VALUES ('','$fullname','$username','$password','$useremail','$date','$level','".$_FILES['file']['tmp_name'],"','$gender')");

            echo "<script type=\"text/javascript\">"; 
            echo "alert('You have been registered!');";
            echo "window.location='forum.php'";
            echo "</script>";
            }
            }
        }

}
else 
    $passErr = "* Your password do not match!"; 
    }
}
}
else
$fillErr = "* Please fill in all fields!";
}
?>

Upvotes: 2

Views: 215

Answers (1)

Basheer Kharoti
Basheer Kharoti

Reputation: 4302

It's not a good idea to save images into database. Upload the images to the directory you wish and just save the path in database.

 $destination = 'YourdesireDestination/';
$fileName = $_FILES['image']['tmp_name'];
 move_uploaded_file($fileName, $destination);

And the save the path to database.

$queryreg = mysql_query("INSERT INTO users (id,name,username,password,email,date,level,image,gender)VALUES ('','$fullname','$username','$password','$useremail','$date','$level','".$destination.$fileName "','$gender')");

Or if you want to save image to db then

Try this

   $image = addslashes(file_get_contents($_FILES['image']['tmp_name']));

and then

 $queryreg = mysql_query("INSERT INTO users (id,name,username,password,email,date,level,image,gender)VALUES ('','$fullname','$username','$password','$useremail','$date','$level','$image','$gender')");

Upvotes: 2

Related Questions