gpt4 book ai didi

php - 图片在mysql数据库中上传显示

转载 作者:行者123 更新时间:2023-11-29 02:45:28 25 4
gpt4 key购买 nike

我需要在mysql数据库中上传一张图片,需要时它应该显示在表格中。我正在使用以下代码存储在数据库中。

<input type="file" name="photo" id="photo">

<input type="submit" value="Save" name="submit">

<?php
mysql_connect("localhost", "root", ""); mysql_select_db("prs");
if(isset($_POST['submit']))
{
$errors= array();
$file_name=$_FILES['photo']['name'];
$file_size =$_FILES['photo']['size'];
$file_tmp =$_FILES['photo']['tmp_name'];
$file_type=$_FILES['photo']['type'];
$file_ext=strtolower(end(explode('.',$_FILES['photo']['name'])));
$expensions= array("jpeg","jpg","png");
if(in_array($file_ext,$expensions)=== false){
$errors[]="extension not allowed, please choose a JPEG or PNG file.";
}
if($file_size > 2097152){
$errors[]='File size must be excately 2 MB';
}
if(empty($errors)==true){
move_uploaded_file($file_tmp,"images/".$file_name);
echo '<script language="javascript">';
echo 'alert("Success")';
echo '</script>';
}
else
{
echo '<script language="javascript">'; echo 'alert("Failed")';
echo '</script>';
print_r($errors);
}
}
$photo = $_FILES['photo'];
{
$query = mysql_query("insert into e (photo) values ('$photo')");
echo '<script language="javascript">'; echo 'alert("Employed")';
echo '</script>';
}
else
{
echo'<script language="javascript">';echo 'alert("Insertions Failed")';
echo '</script>';
}
}
mysql_close($connection);
?>

下面的代码用于在表单加载时显示来自数据库的图像。

<form method="get" enctype="multipart/form-data">
<table class="table table-bordered table-striped">
<thead><tr><th>Photo</th></tr></thead>
<tbody><tr>
<?php
$connection = mysql_connect("localhost", "root", "");
$db = mysql_select_db("prs", $connection);
$sql = "SELECT * FROM e";
$result = mysql_query($sql);
while ($outw = mysql_fetch_assoc($result))
{
echo "<tbody>";
echo "<tr>"; echo "<td>".'<img src="data:image/jpeg;base64,'.base64_encode( $result['photo'] ).'"/>' ."</td>"; echo "</tr>";
echo "</tbody>";
}
?>
</tr>
</tbody>
</table>
</form>

我使用博客作为照片的数据类型。上面的代码没有显示来自数据库的图像。

最佳答案

这里你需要获取图像数据并以BLOB形式保存在数据库中:

$photo = file_get_contents($_FILES['photo']['tmp_name']);

Can I store images in MySQL

关于php - 图片在mysql数据库中上传显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43013678/

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