我在这里读了大约15个线程,每个线程都试过了,但都没有成功,问题似乎围绕着使用blob上传图片(是的,我知道我不应该这样做,因为它的规模会导致一个问题,但这只是一个原型)。似乎即使我正在传递文件,$\u files['picture']没有设置。如果我删除if并得到类似$imagename=$\u files['picture']['name'];它说索引图片不存在。任何帮助都将不胜感激,谢谢。
这是我的html代码:
<!DOCTYPE html>
<html>
<head>
<?php include ('header.php'); ?>
<title>Add item</title>
</head>
<body>
<h1>Add item:</h1>
<form method="post" action="additem.php">
Item Name:
<input type="varchar" name="name" maxlength=64>
<br><br>
Description:<br>
<textarea name="description">
</textarea>
<br><br>
Date added:
<input type="date" name="date">
<br><br>
Picture:
<input type="file" name="picture" value="picture">
<br><br>
Shop:
<input type="varchar" name="shop" maxlength=64>
<br><br>
Type:
<input type="varchar" name="type" maxlength=15>
<br><br>
subtype:
<input type="varchar" name="subtype" maxlength=32>
<br><br>
<input type="submit" enctype="multipart/form-data" method="post" name="submit" value="Add event">
</form>
<br><br>
以及我的php代码:
<?php
if (empty($_SESSION['username'])) {
echo "Not logged in";
exit;
} else {
if ($_SESSION['privilege'] == "student") {
echo $_SESSION['username'];
echo " does not have permission to create events ";
exit;
} else {
echo $_SESSION['username'];
echo " creating new item";
}
}
if(!empty($_POST)) {
require_once('connectdb.php');
$name = $_POST['name'];
if(empty($name)) {
echo(" You must enter an item name.");
exit;
}
$date = $_POST['date'];
if(empty($date)) {
echo(" You must enter an item add date.");
exit;
}
$shop= $_POST['shop'];
if(empty($venue)) {
echo(" You must enter an shop.");
exit;
}
if (isset($_FILES['picture'])){
$imageName = $_FILES['picture']['name'];
$imageData = $_FILES['picture']['tmp_name'];
$imageType = $_FILES['picture']['type'];
if(substr($imageType,0,5) == "image")
{
echo "this is an image";
} else {
echo "Incorrect file type";
exit;
}
$description = $_POST['description'];
$type = $_POST['type'];
$subtype = $_POST['subtype'];
$organiser = $_SESSION['username'];
try {
$stmt = $db->prepare("INSERT INTO `items` (`name`, `description`, `date`, `picturename`, `picture`, `organiser`, `shop`, `type`, `subtype`) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)");
$stmt->execute(array($name, $description, $date, $imageName, $imageData, $_SESSION['username'], $shop, $type, $subtype));
echo("Successful.");
} catch(PDOException $ex) {
echo("Failed to save data to database.<br>");
echo($ex->getMessage());
exit;
}
}else{
echo "file not set";
}
}
?>
</body>
</html>
1条答案
按热度按时间fhity93d1#
您应该为表单指定非默认编码-否则浏览器根本不会上载您的文件: