上传的图像文件扩展名未存储在数据库中

时间:2015-08-19 10:44:17

标签: php mysql

这里我试图将图像上传到目录并在数据库中添加路径。 当我填写表单并单击提交时,图像会上传到目录,但文件扩展名不会保存在数据库中。

例如,如果我按名称mobile-image-small-1.png上传图片,则数据库中的文件名将仅为mobile-image-small-1,并且导致图片无法在网页上显示。这里的问题是什么

我正在使用mysql数据库

这是我的PHP代码     

// Escape user inputs for security
$product_name = mysqli_real_escape_string($link, $_POST['product_name']);
$product_category = mysqli_real_escape_string($link, $_POST['product_category']);
$product_price = mysqli_real_escape_string($link, $_POST['product_price']);
$pro_url = mysqli_real_escape_string($link, $_POST['pro_url']);
$co_owners = mysqli_real_escape_string($link, $_POST['co_owners']);
// attempt insert query execution
// close connection

// Check connection
if($link === false){
    die("ERROR: Could not connect. " . mysqli_connect_error());
}
if($_POST)
{ 
if($_FILES["file"]["error"] > 0)
{
echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
}
else
{
if(file_exists("uploaded_images/" . $_FILES["file"]["name"]))
{
echo $_FILES["file"]["name"] . " already exists. ";
}
else
{
if(move_uploaded_file($_FILES["file"]["tmp_name"],"uploaded_images/" . $_FILES["file"]["name"]))
{
$sql = "INSERT INTO product_list (product_name, product_category, product_price,product_referrence_URL,product_co_owners,product_image_url) VALUES ('$product_name', '$product_category', '$product_price', '$pro_url', '$co_owners', 'files/uploaded_images/".$_FILES['file']['name']."')";
if(mysqli_query($link, $sql)){
    header("Location: ../index.php");   
}else{
 echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}
if(mysqli_query($sql))
{
echo "Stored in: " . "uploaded_images/" . $_FILES["file"]["name"];
}
else
{
echo 'Unable to store';
}
}
}
}
}
mysqli_close($link);
?>

这是我的表格

<form class="form-horizontal" method="post" action="files/insert.php" role="form" enctype="multipart/form-data">
              <div class="form-group">
                <label class="control-label col-sm-2" for="product_name">Product Name</label>
                <div class="col-sm-10">
                  <input type="text" class="form-control" name="product_name" id="product_name" placeholder="Iphone 5c" required>
                </div>
              </div>

              <div class="form-group">
                <label class="control-label col-sm-2" for="pwd">Product Category</label>
                <div class="col-sm-10">                            
                  <select class="btn-btn-primary form-control" name="product_category">
                    <option>Mobile</option>
                    <option>Television</option>
                    <option>Printer</option>
                    <option>Watch</option>
                    <option>Monitor</option>
                  </select>
                </div>
              </div>

              <div class="form-group">
                <label class="control-label col-sm-2" for="product-pic">Upload your profile picture</label>
                <div class="col-sm-10">          
                  <input type="file" class="form-control" name="file" id="file" /> 
                </div>
              </div>


              <div class="form-group">
                <label class="control-label col-sm-2" for="product_price">Product Price</label>
                <div class="col-sm-10">
                  <input type="text" class="form-control" name="product_price" id="product_price" placeholder="Rs.36,000" required>
                </div>
              </div>


              <div class="form-group">
                <label class="control-label col-sm-2" for="pro_url">Reference URL</label>
                <div class="col-sm-10">
                  <input type="URL" class="form-control" name="pro_url" id="pro_url" placeholder="http://www.amazon.com" required>
                </div>
              </div>


              <div class="form-group">
                <label class="control-label col-sm-2" for="co_owners">Co-owners</label>
                <div class="col-sm-10">
                    <select class="btn-btn-primary form-control" name="co_owners">
                        <option>Select no. owners</option>
                        <option>1</option>
                        <option>2</option>
                        <option>3</option>
                        <option>4</option>
                        <option>5</option>
                        <option>6</option>
                        <option>7</option>
                        <option>8</option>
                        <option>9</option>
                        <option>10</option>
                        <option>11</option>
                        <option>12</option>
                        <option>13</option>
                        <option>14</option>
                        <option>15</option>
                    </select>                      
                </div>
              </div>


              <div class="form-group">        
                <div class="col-sm-offset-2 col-sm-10">
                  <button type="submit" class="btn btn-success">Add product</button>
                </div>
              </div>
            </form>

2 个答案:

答案 0 :(得分:1)

根据讨论,您的数据库length is 30 and type is varchar和您的图像长度大于30.这就是您将数据的一半更新到数据库的原因。

因此,将列的长度增加到100,将长度uo设置为100长度。

答案 1 :(得分:0)

我认为您可以使用类型字段来获取此值

 $_FILE['file']['type'] 

查看文档here