所以我寫了這個腳本,我一直未能上傳消息,請指出我做錯了什麼地方,請爲腳本舉個好例子。該圖像沒有上傳到數據庫,但它被移動到目錄'upload /'。 $ start & $ stop變量是日期。我在這一天呆了好幾天。這是代碼。無法上傳圖像到數據庫
include "../config/database.php";
$title = $_POST['title'];
$id = $_POST['id'];
$genre = $_POST['genre'];
$start = $_POST['start'];
$stop = $_POST['stop'];
$description = $_POST['description'];
$target_dir = "upload/";
$target_file = $target_dir . basename($_FILES["image"]["name"]);
$uploadOK = 1;
$imagetype = pathinfo($target_file, PATHINFO_EXTENSION);
if (isset($_POST["submit"])) {
$check = getimagesize($_FILES["image"]["tmp_name"]);
if ($check !== false) {
echo "File is an image -" . $check["mime"] . ".";
$uploadOK = 1;
} else{
echo "File isn't an image.";
$uploadOK = 0;
}
}
if (file_exists($target_file)) {
echo "Sorry, file is already exist.";
$uploadOK = 0;
}
if ($_FILES["image"]["size"] > 5000000) {
echo "Sorry, file is too large.";
$uploadOK = 0;
}
if ($imagetype != "jpg" && $imagetype != "jpeg" && $imagetype != "png" && $imagetype != "gif") {
echo "Sorry, only JPEG, JPG, PNG and GIF are allowed.";
$uploadOK = 0;
}
if ($uploadOK == 0) {
echo "Failed to upload.";
} else {
if(move_uploaded_file($_FILES["image"]["tmp_name"] , $target_file)){
$query = mysql_query("INSERT INTO anidata (id, title, image, genre, start, stop, description) VALUES ('$title', '$id', '$target_file', '$genre', '$start', '$stop', '$description') ");
$uploadOK = 1;
if ($query) {
header("Location: view.php");
}else{
echo "<p>Failed to upload</p>";
}
}
}
我真的很感激任何幫助,謝謝:)
[請不要在新代碼中使用'mysql_ *'函數](http://stackoverflow.com/q/12859942/1190388)。他們不再被維護,並[正式棄用](https://wiki.php.net/rfc/mysql_deprecation)。改爲了解準備好的語句,然後使用[tag:PDO]或[tag:MySQLi]。 – hjpotter92 2014-11-24 01:33:48