我正在制作一个简单的脚本来上传图片,但有些东西对我不起作用..我尝试过evrything,但我无法弄清楚为什么文件没有保存在服务器上以及为什么路径没有保存在mysql中。 。
PS:没有PHP错误..
submit.php
<form onSubmit="return validateForm()" action="offer_save.php" method="post" class="form-horizontal form-label-left input_mask">
<div class="form-group">
<label class="control-label col-md-3 col-sm-3 col-xs-12" for="title">Title: <span class="required">*</span></label>
<div class="col-md-6 col-sm-6 col-xs-12">
<input type="text" name="title" required="required" class="form-control col-md-7 col-xs-12">
</div>
</div>
<div class="form-group">
<label class="control-label col-md-3 col-sm-3 col-xs-12" for="start_day">Offer Start: <span class="required">*</span></label>
<div class="col-md-6 col-sm-6 col-xs-12">
<select name="start_day" id="start_date">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
<option value="10">10</option>
<option value="11">11</option>
<option value="12">12</option>
<option value="13">13</option>
<option value="14">14</option>
<option value="15">15</option>
<option value="16">16</option>
<option value="17">17</option>
<option value="18">18</option>
<option value="19">19</option>
<option value="20">20</option>
<option value="21">21</option>
<option value="22">22</option>
<option value="23">23</option>
<option value="24">24</option>
<option value="25">25</option>
<option value="26">26</option>
<option value="27">27</option>
<option value="28">28</option>
<option value="29">29</option>
<option value="30">30</option>
<option value="31">31</option>
</select>
<select name="start_month" id="start_date">
<option value="01">January</option>
<option value="02">February</option>
<option value="03">March</option>
<option value="04">April</option>
<option value="05">May</option>
<option value="06">June</option>
<option value="07">July</option>
<option value="08">August</option>
<option value="09">September</option>
<option value="10">October</option>
<option value="02">November</option>
<option value="12">December</option>
</select>
<select name="start_year" id="start_date">
<option value="2016" selected="selected">2016</option>
<option value="2017">2017</option>
</select>
</div>
</div>
<div class="form-group">
<label class="control-label col-md-3 col-sm-3 col-xs-12" for="end_day">Offer Start: <span class="required">*</span></label>
<div class="col-md-6 col-sm-6 col-xs-12">
<select name="end_day" id="start_date">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
<option value="10">10</option>
<option value="11">11</option>
<option value="12">12</option>
<option value="13">13</option>
<option value="14">14</option>
<option value="15">15</option>
<option value="16">16</option>
<option value="17">17</option>
<option value="18">18</option>
<option value="19">19</option>
<option value="20">20</option>
<option value="21">21</option>
<option value="22">22</option>
<option value="23">23</option>
<option value="24">24</option>
<option value="25">25</option>
<option value="26">26</option>
<option value="27">27</option>
<option value="28">28</option>
<option value="29">29</option>
<option value="30">30</option>
<option value="31">31</option>
</select>
<select name="end_month" id="start_date">
<option value="01">January</option>
<option value="02">February</option>
<option value="03">March</option>
<option value="04">April</option>
<option value="05">May</option>
<option value="06">June</option>
<option value="07">July</option>
<option value="08">August</option>
<option value="09">September</option>
<option value="10">October</option>
<option value="02">November</option>
<option value="12">December</option>
</select>
<select name="end_year" id="start_date">
<option value="2016" selected="selected">2016</option>
<option value="2017">2017</option>
</select>
</div>
</div>
<div class="form-group">
<label class="control-label col-md-3 col-sm-3 col-xs-12" for="description">Description <span class="required">*</span></label>
<div class="col-md-6 col-sm-6 col-xs-12">
<textarea name="description" required="required" class="form-control col-md-7 col-xs-12"></textarea>
</div>
</div>
<div class="form-group">
<label class="control-label col-md-3 col-sm-3 col-xs-12" for="category">Industry <span class="required">*</span></label>
<div class="col-md-6 col-sm-6 col-xs-12">
<select name="category" id="category" class="form-control">
<option value="Automotive">Automotive</option>
<option value="Business & Economy">Business & Economy</option>
<option value="Careers & Jobs">Careers & Jobs</option>
<option value="Computers">Computers</option>
<option value="Education & Training">Education & Training</option>
<option value="Entertainment & Media">Entertainment & Media</option>
<option value="Health & Beauty Care">Health & Beauty Care</option>
<option value="Industry">Industry</option>
<option value="Internet & WWW">Internet & WWW</option>
<option value="Law">Law</option>
<option value="Life">Life</option>
<option value="Real Estate">Real Estate</option>
<option value="Science">Science</option>
<option value="Shopping & Services">Shopping & Services</option>
<option value="Small Business">Small Business</option>
<option value="Society">Society</option>
<option value="Sports">Sports</option>
<option value="Telecommunications">Telecommunications</option>
<option value="Travel & Recreation">Travel & Recreation</option>
</select>
</div>
</div>
<div class="form-group">
<label class="control-label col-md-3 col-sm-3 col-xs-12" for="price">Price (in €): <span class="required">*</span></label>
<div class="col-md-6 col-sm-6 col-xs-12">
<input type="text" name="price" required="price" class="form-control col-md-7 col-xs-12">
</div>
</div>
<p>
<label for="fileToUpload">Thumb:</label>
<input type="file" name="fileToUpload" id="fileToUpload">
</p>
<input type="hidden" name="user_id" size="30" maxlenght="40" value="<?php echo $_SESSION['user_id']; ?>" />
<div class="form-group center">
<button type="submit" name="send" id="send" class="btn btn-primary">Submit Offer</button>
</div>
</form>
offer_save.php
session_start();
include_once('db_connect.php');
include_once('classes.php');
$offer_user = $_POST['user_id'];
$offer_title = $_POST['title'];
$offer_start = $_POST['start_year'] ."-" . $_POST['start_month']. "-" . $_POST['start_day'];
$offer_end = $_POST['end_year'] ."-" . $_POST['end_month']. "-" . $_POST['end_day'];
$offer_description = $_POST['description'];
$offer_category = $_POST['category'];
$offer_link = $_POST['link'];
$offer_price = $_POST['price'];
$offer_created = date("Y-m-d H:i:s");
if(isset($_FILES["fileToUpload"]["name"]) && !empty($_FILES["fileToUpload"]["name"])){
$firmen_logo_path = firmenLogoUpload();
}
else {
//default logo
$firmen_logo_path ="../uploads/firmen_logo/porsche_logo.png";
}
$query = "INSERT INTO ics_offers (offer_user_id, offer_title, offer_start, offer_end, offer_description, offer_created, offer_category, offer_price, offer_link, offer_thumb) VALUES ('$offer_user','$offer_title','$offer_start','$offer_end','$offer_description','$offer_created','$offer_category','$offer_price','$offer_link','$firmen_logo_path')";
$result = mysqli_query($link,$query);
$home = "../src/my_offers.php?offer_saved=ok";;
header('Location:' . $home);
classes.php
function firmenLogoUpload(){
$target_dir = "../uploads/firmen_logo/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
// Check if image file is a actual image or fake image
if(isset($_POST["send"]))
{
$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;
}
}
// Check if file already exists
if (file_exists($target_file))
{
// echo "Sorry, file already exists.";
$uploadOk = 0;
}
// Check file size
if ($_FILES["fileToUpload"]["size"] > 5000000)
{
//echo "Sorry, your file is too large.";
$uploadOk = 0;
}
// Allow certain file formats
if($imageFileType != "jpg" && $imageFileType != "JPG" && $imageFileType != "JPEG" && $imageFileType != "png" && $imageFileType !=
"PNG" && $imageFileType != "jpeg"
&& $imageFileType != "gif" )
{
}
// 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)== true)
{
return $target_file;
} else
{
return $target_file;
}
}
答案 0 :(得分:0)
替换<form onSubmit="return validateForm()" action="offer_save.php" method="post" class="form-horizontal form-label-left input_mask">
使用<form onSubmit="return validateForm()" action="offer_save.php" method="post" class="form-horizontal form-label-left input_mask" enctype="multipart/form-data" >
通过使用
转义字符串来清理用户输入mysqli_real_escape_string($vairable)