如何在PHP中上传图像?

时间:2015-11-11 07:08:55

标签: php

我尝试上传图片但不成功!除图像外,所有信息都将插入数据库。我尝试使用文本和blob类型的图像之前也没有成功。我只能使用mysql因为我的所有编码都使用mysql插入数据库而不是mysqli。

以下是我的注册码

<?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!";
}
?>

1 个答案:

答案 0 :(得分:2)

将图像保存到数据库中并不是一个好主意。将图像上传到您希望的目录,然后将路径保存在数据库中。

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

并保存到数据库的路径。

$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')");

或者如果你想将图像保存到db那么

试试这个

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

然后

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