我試圖用PHP中的表單上傳圖片,以便將信息保存到數據庫中,但圖片保存到文件夾中。我有'$ pic'變量作爲'BLOB',但是當我上傳數據庫告訴我它是0kb,並且文件夾中沒有圖片。我是很新,PHP,所以我希望你明白我的意思:d THX上傳圖像到文件夾和使用PHP/SQL的數據庫信息
下面的代碼:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert car!</title>
<link rel="stylesheet" href="css/newgame.css"/>
</head>
<body>
<?php
require_once "includes/connection.php";
$target_dir = "img/";
$target_file = $target_dir . basename($_FILES['pic']['name']);
$car = mysqli_real_escape_string($con, $_POST['car']);
$descr = mysqli_real_escape_string($con, $_POST['description']);
$manuf = mysqli_real_escape_string($con, $_POST['manuf']);
$dist = mysqli_real_escape_string($con, $_POST['dist']);
$price = mysqli_real_escape_string($con, $_POST['price']);
$name = mysqli_real_escape_string($con, $_POST['name']);
$pic = mysqli_real_escape_string($con, $_FILES['pic']['name']);
move_uploaded_file($_FILES["pic"]["tmp_name"], $target_file);
mysqli_query($con, "INSERT INTO carlist (pic, car, descr, year, dist, price, seller) VALUES ('$pic', '$car', '$descr', '$price', '$manuf', '$dist', '$name')");
echo "<p>The following car information was successfully added to database:</p>";
echo "<p>\"$car\"</p>";
echo "<p>Description: $descr</p>";
echo "<p>Price: $price</p>";
echo "<p>Manufacturer: $manuf</p>";
echo "<p>Picture: $pic</p>";
echo "<p>Name: $name</p>";
echo "<p>Distance: $dist</p>";
mysqli_close($con);
?>
<br><br>
<a href="newcar.html">Insert another car</a>
<br>
<a href="index.php">Home</a>
</body>
</html>
請不要將圖像保存到文件服務器並將圖像路徑保存到數據庫是「更好的做法」,而不是將圖像實際存儲在數據庫中。 – 2015-04-02 09:00:06
對此感到抱歉。這就是我想要做的。 – Chumppe 2015-04-02 09:17:54
你可以顯示HTML你的表格 – 2015-04-02 09:20:55