简体   繁体   中英

store path of image uploaded on server

<?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); 
?>

I have this script that takes input from user and uploads the images on server folder, but i also wish to save the path of the image in the database. however i am not able to do so, can anyone please guide me with this code

You've got an extra . in there. Like you were going to concatenate the string but nothing followed. Remove the . right before the semicolon.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM