我想将数据发送到数据库,所以当用户上传图像时,他的所有图像数据都保存在mysql的数据库中,请告诉我应该怎么做才能使它工作。
当用户上传图片时,他可以在成功上传后看到此类数据
Upload: wallpaper.jpg
Type: image/jpeg
Size: 8.7072027134876 kB
Stored in: upload/wallpaper.jpg
所以我想将这些数据保存在数据库中,例如Type in type列,size in size列和类似的链接列中的图像链接
这是我的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>
答案 0 :(得分:-1)
添加:
$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_connect
和mysql_select_db
。当然,您的表格中应该有size
,type
,link
列