我正在尝试使用表单将图像上传到数据库。
问题在于,当我尝试上传图像时,它不会存储在数据库中。也没有错误。
提前致谢。
<html>
<head>
<meta charset="UTF-8" />
<title>Images</title>
</head>
<body>
<form action="index.php" method="POST" enctype="multipart/form-data">
File:
<input type="file" name="image" />
<input type="submit" value="Upload" />
</form>
<?php
require "connect.inc.php";
@$file = $_FILES['image']['tmp_name'];
if (!isset($file)) {
echo "Please select an image.";
}
else {
$image = file_get_contents($_FILES['image']['tmp_name']);
$imageName = $_FILES['image']['tmp_name'];
$imageSize = getimagesize($_FILES['image']['tmp_name']);
if ($imageSize == FALSE) {
echo "This is not an image.";
}
else {
$sql = "INSERT INTO `afbeelding` VALUES ('', '$imageName', '$image')";
if (!mysql_query("INSERT INTO `afbeelding` VALUES ('', '$imageName', '$image')")) {
echo "Problem uploading image.";
}
else {
echo "Succes!";
}
}
}
?>
</body>
</html>
答案 0 :(得分:0)
您忘了执行查询:
<html>
<head>
<meta charset="UTF-8" />
<title>Images</title>
</head>
<body>
<form action="index.php" method="POST" enctype="multipart/form-data">
File:
<input type="file" name="image" />
<input type="submit" value="Upload" />
</form>
<?php
require "connect.inc.php";
@$file = $_FILES['image']['name'];
if (!isset($file)) {
echo "Please select an image.";
}
else {
$image = file_get_contents($_FILES['image']['name']);
$imageName = $_FILES['image']['tmp_name'];
$img = $_FILES['image']['name'];
$imageSize = getimagesize($_FILES['image']['name']);
//add move uploaded file function here
if ($imageSize == FALSE) {
echo "This is not an image.";
}
else {
$sql = mysql_query("INSERT INTO `afbeelding` VALUES ('', '$img', '$image')"); // did changes here
if($sql){
echo "Succes!";
}
else {
echo "Problem uploading image.";
}
}
}
?>
</body>
</html>
答案 1 :(得分:0)
我没有测试,但会工作
<?php
require "connect.inc.php";
if(isset($_POST['submit']))
{
@$file = $_FILES['image']['tmp_name'];
if (!isset($file)) {
echo "Please select an image.";
}
else {
$image = file_get_contents($_FILES['image']['tmp_name']);
$imageName = $_FILES['image']['tmp_name'];
$imageSize = getimagesize($_FILES['image']['tmp_name']);
if ($imageSize == FALSE) {
echo "This is not an image.";
}
else {
$sql = "INSERT INTO `afbeelding` VALUES ('', '$imageName', '$image')";
if (!mysql_query("INSERT INTO `afbeelding` VALUES ('', '$imageName', '$image')")) {
echo "Problem uploading image.";
}
else {
echo "Succes!";
}
}
}
}
?>
<html>
<head>
<meta charset="UTF-8" />
<title>Images</title>
</head>
<body>
<form action="index.php" method="POST" enctype="multipart/form-data">
File:
<input type="file" name="image" />
<input type="submit" name="submit" value="Upload" />
</form>
</body>
</html>
答案 2 :(得分:0)
问题是没有addslashes()。在:$ image = addslashes(file_get_contents($ _ FILES ['image'] ['tmp_name']));
如果我再次移除它们将无法正常工作,但如果我将它们重新放入其中则成功。