【发布时间】:2014-11-24 01:27:14
【问题描述】:
所以我写了这个脚本,但我一直无法上传消息,请指出我做错的地方,并为脚本提供一个很好的例子。图片未上传到数据库,但已移至目录“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>";
}
}
}
非常感谢任何帮助,谢谢:)
【问题讨论】:
-
Please, don't use
mysql_*functions 在新代码中。它们不再被维护并且是officially deprecated。改为了解准备好的语句,并使用 pdo 或 mysqli。