gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 23:53:58 25 4
gpt4 key购买 nike

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

例如,如果我按名称 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>

最佳答案

根据讨论,您的数据库长度为 30,类型为 varchar,您的图像长度大于 30。这就是您将一半数据更新到数据库的原因。

因此,将列的长度增加到 100,需要长度 uo 到 100。

关于php - 上传的图像文件扩展名未存储在数据库中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32093279/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com