将图像上载数据发送到数据库


Send Image Upload data to Database

我想把数据发送到数据库,所以当用户上传图像时,他所有的图像数据都保存在mysql中的数据库中,请告诉我该怎么做才能让它工作。

当用户上传图像时,他可以在成功上传后看到这种类型的数据

Upload: wallpaper.jpg
Type: image/jpeg
Size: 8.7072027134876 kB
Stored in: upload/wallpaper.jpg 

所以我想把这些数据保存在数据库中,比如Type in Type列、size in size列和类似的image link in link列

这是我的PHP代码:

<?php
$username = "root";
    $password = "123";
    $hostname = "localhost";
    $dbhandle = mysql_connect($hostname, $username, $password) or die("Could not connect to database");
    $selected = mysql_select_db("user", $dbhandle);
$allowedExts = array("gif", "jpeg", "jpg", "png");
$temp = explode(".", $_FILES["file"]["name"]);
$extension = end($temp);
if (($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/jpg")
|| ($_FILES["file"]["type"] == "image/pjpeg")
|| ($_FILES["file"]["type"] == "image/x-png")
|| ($_FILES["file"]["type"] == "image/png")
&& ($_FILES["file"]["size"] <= 200000)
&& in_array($extension, $allowedExts))


  {
  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"] / 10024) . " kB<br>";
    if (file_exists("upload/" . $_FILES["file"]["name"]))
      {
      echo $_FILES["file"]["name"] . " already exists. ";
      }
    else
      {
      move_uploaded_file($_FILES["file"]["tmp_name"],
      "upload/" . $_FILES["file"]["name"]);
      echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
      }
    }
  }
else
  {
  echo "Invalid file";
  }
?> 

这是HTML代码

<html>
<body>
<form action="upload_file.php" method="post"
enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="file" id="file"><br>
<input type="submit" name="submit" value="Submit">
</form>
</body>
</html> 

添加:

$link="upload/" . $_FILES["file"]["name"];
$filetype=$_FILES["file"]["type"];
$filesize=$_FILES["file"]["size"];
$sql=mysql_query("INSERT INTO `tblname` (`size`,`type`,`link`) VALUES ('$filesize','$filetype','$link')") or die(mysql_error());

之后:

echo "Stored in: " . "upload/" . $_FILES["file"]["name"];

当然,在保存数据之前,您应该先初始化mysql_connectmysql_select_db。当然,您的表中应该有sizetypelink