我有一个管理页面,管理员将在表单中插入新产品。我无法上传图片,收到未定义的fileToUpload错误。我需要上传图像并将图像名称存储到数据库中。
这是我的代码:
session_start();
include_once '../inc/config.php';
if(!isset($_SESSION['admin']))
{
header('Location: login.php');
exit;
}
if(isset($_POST['insert']))
{
$cname = mysqli_real_escape_string($connect, $_POST['cname']);
$ccost = mysqli_real_escape_string($connect, $_POST['ccost']);
$ctype = mysqli_real_escape_string($connect, $_POST['ctype']);
$cimage = mysqli_real_escape_string($connect, $_POST['cimage']);
$ccapa = mysqli_real_escape_string($connect, $_POST['ccapa']);
$cstatus = mysqli_real_escape_string($connect, $_POST['cstatus']);
$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
$check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
if($check !== false) {
echo "File is an image - " . $check["mime"] . ".";
$uploadOk = 1;
} else {
echo "File is not an image.";
$uploadOk = 0;
}
if (file_exists($target_file)) {
echo "Sorry, file already exists.";
$uploadOk = 0;
}
// Check file size
if ($_FILES["fileToUpload"]["size"] > 500000) {
echo "Sorry, your file is too large.";
$uploadOk = 0;
}
// Allow certain file formats
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType
!= "jpeg"
&& $imageFileType != "gif" ) {
echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
$uploadOk = 0;
}
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
echo "Sorry, your file was not uploaded.";
// if everything is ok, try to upload file
} else {
if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"],
$target_file)) {
echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";
} else {
echo "Sorry, there was an error uploading your file.";
}
}
if(mysqli_query($connect, "INSERT INTO cars(car_name, car_type, image, rent_cost, capacity, status) VALUES
('".$cname."', '".$ctype."', '". basename( $_FILES["fileToUpload"]["name"]). "', '".$ccost."', '".$ccapa."', '".$cstatus."')")){
$success = "<div class='alert alert-success'><strong>Successfully Inserted!</strong></div>";
}else{
$errormsg = "<div class='alert alert-danger'>Error inserting item.</div>";
}
}
我的HTML代码:
<div id="insert" class="collapse">
</br>
<form class="form-horizontal" method="post" action="">
<fieldset>
<!-- Form Name -->
<legend>Insert an Item</legend>
<!-- Text input-->
<div class="form-group">
<label class="col-md-4 control-label" for="textinput">Car Name</label>
<div class="col-md-4">
<input id="textinput" name="cname" type="text" placeholder="Enter car name.." class="form-control input-md" required="">
</div>
</div>
<!-- Text input-->
<div class="form-group">
<label class="col-md-4 control-label" for="textinput">Rent Cost</label>
<div class="col-md-4">
<input id="textinput" name="ccost" type="number" placeholder="Enter rent cost.." class="form-control input-md" required="">
</div>
</div>
<!-- Text input-->
<div class="form-group">
<label class="col-md-4 control-label" for="textinput">Car Type</label>
<div class="col-md-4">
<input id="textinput" name="ctype" type="text" placeholder="Enter car type.." class="form-control input-md" required="">
</div>
</div>
<!-- File Button -->
<div class="form-group">
<label class="col-md-4 control-label" for="filebutton">Image File</label>
<div class="col-md-4">
<input id="filebutton" name="fileToUpload" class="input-file" type="file">
</div>
</div>
<!-- Text input-->
<div class="form-group">
<label class="col-md-4 control-label" for="textinput">Capacity</label>
<div class="col-md-4">
<input id="textinput" name="ccapa" type="number" placeholder="Enter car capacity.." class="form-control input-md" required="">
</div>
</div>
<!-- Select Basic -->
<div class="form-group">
<label class="col-md-4 control-label" for="selectbasic">Status</label>
<div class="col-md-4">
<select id="selectbasic" name="cstatus" class="form-control">
<option value="Available">Available</option>
<option value="Not Avaialble">Not Available</option>
</select>
</div>
</div>
<!-- Button -->
<div class="form-group">
<label class="col-md-4 control-label" for="singlebutton"></label>
<div class="col-md-4">
<button id="singlebutton" name="insert" class="btn btn-success">Insert</button>
</div>
</div>
</fieldset>
</form>
</div>
&#13;
谢谢!
答案 0 :(得分:0)
尝试以下方法:
enctype="multipart/form-data"
添加到您的表单标记print_r($_FILES)
查看是否
$_FILES["fileToUpload"]
已定义答案 1 :(得分:0)
您需要在{form}之后的void main (string args[]) {
string ui_string = """
<?xml version="1.0" encoding="UTF-8"?>
<interface>
<object class="MyCustomWidget">
</object>
</interface>
""";
Gtk.init (ref args);
// create a throw-away instance of MyCustomWidget
var tmp = new MyCustomWidget ();
Gtk.Builder builder = new Gtk.Builder.from_string (ui_string, ui_string.length);
}
中添加enctype="multipart/form-data"
。否则,发送到服务器的数据将无法识别。
答案 2 :(得分:0)
感谢那些回答我问题的人。我已经解决了我的问题。
$check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
if($check !== false) {
echo "File is an image - " . $check["mime"] . ".";
$uploadOk = 1;
} else {
echo "File is not an image.";
$uploadOk = 0;
}
并修复了它。