存储上传到服务器上的图像路径

时间:2014-11-24 17:31:02

标签: php mysql sql mysqli phpmyadmin

<?php
ob_start();
$con=mysqli_connect("localhost","root","","db");
// Check connection
if (mysqli_connect_errno()) {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

$file_exts = array("jpg", "bmp", "jpeg", "gif", "png");
$upload_exts = end(explode(".", $_FILES["file"]["name"]));
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/png")
|| ($_FILES["file"]["type"] == "image/pjpeg"))
&& ($_FILES["file"]["size"] < 2000000)
&& in_array($upload_exts, $file_exts))
    {
        if ($_FILES["file"]["error"] > 0)
            {
                echo "Return Code: " . $_FILES["file"]["error"] . "<br>";
            }
        else
            {
                //echo "Upload: " . $_FILES["file"]["name"] . "<br>";
                //echo "Type: " . $_FILES["file"]["type"] . "<br>";
                //echo "Size: " . ($_FILES["file"]["size"] / 1024) . " kB<br>";
                //echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br>";
                // Enter your path to upload file here
                if (file_exists("uploads".$_FILES["file"]["name"]))
                    {
                        echo "<div class='error'>"."(".$_FILES["file"]["name"].")". " already exists. "."</div>";
                    }
                else
                    {
                        move_uploaded_file($_FILES["file"]["tmp_name"],"uploads/" . $_FILES["file"]["name"]);

                        $imagepath = "uploads/" . $_FILES["file"]["name"].;

                        $sql = "UPDATE register SET imagepath='$imagepath' WHERE id='$user_coid' ";
                            if (!mysqli_query($con,$sql)) 
                                {
                                    die('Error: ' . mysqli_error($con));
                                }
                    }
            }
    }
else
    {
        echo "<div class='error'>Invalid file</div>";
    }
mysqli_close($con); 
?>

我有这个脚本从用户接收输入并上传服务器文件夹上的图像,但我也希望保存图像的路径在数据库中。但是我无法这样做,任何人都可以用这段代码指导我

1 个答案:

答案 0 :(得分:0)

你还有一个额外的.。就像你要连接字符串但没有任何内容。在分号前面删除.

$imagepath = "uploads/" . $_FILES["file"]["name"];