【问题标题】:Upload Image using Php and mySQL使用 PHP 和 mySQL 上传图片
【发布时间】:2025-12-10 07:10:01
【问题描述】:

我正在尝试使用 php 上传图像并保存在 mysql 数据库中,该数据库将接受它的不同扩展名,例如 bmp、jpeg 等。通过使用以下代码,一些上传的图像显示不完整。

这是上传表单:

<html>
  <form method="post" action="updateImage1.php" enctype="multipart/form-data">
  <table border=0>
  <tr>
    <td><center><img src="getImage.php?id='.$row["No"].'" width=250 height=180/></center><br>      
    <input type="file" name="s4"><br>
    <input name="update" type="submit" id="update" value="Save Changes" class="btn btn-primary" >
    &nbsp &nbsp
     </form><a href="admin3.php"><button type="button" class="btn btn-primary">Cancel</button></a>
 </tr>

 </table>
 </html>

这是updateImage1.php:

<?php
$s1 = addslashes(file_get_contents($_FILES['s4']['tmp_name']));

$host="localhost";
$user_name="root";
$database_name="5r";
$db=mysql_connect($host, $user_name,'');
if (mysql_error() > "") echo mysql_error() . "<br>";
mysql_select_db($database_name, $db);
if (mysql_error() > "") echo mysql_error() . "<br>";

$query = "UPDATE tblMain SET images='$s1' WHERE No=3";
$qresult = mysql_query($query);
echo "<script>alert('Records Successfully Updated'); location.href='admin3.php';</script>";
?>

这是getImage.php:

 <?php

 $No = $_GET['id'];
 $link = mysql_connect("localhost", "root", "");
 mysql_select_db("5r");
 $sql = "SELECT images FROM tblMain WHERE No=$No";
 $result = mysql_query("$sql");
 $row = mysql_fetch_assoc($result);
 mysql_close($link);

 header("Content-type: image/jpeg/bmp/png");
 echo $row['images'];
 ?>

【问题讨论】:

  • 1) db里面的insert在哪里? 2) mysql_* 已弃用,3) 我们在谈论多大的图像?
  • 我怀疑这是有效的header("Content-type: image/jpeg/bmp/png"); 按照手册php.net/manual/en/function.image-type-to-mime-type.php - 我建议你看看*.com/a/2634072
  • 我没有使用 Insert 语句,因为我正在尝试更新数据库中上传的图像。但是每次我上传一张在picasa照片查看器中编辑过并且有bmp扩展名的图片时,图片显示不完整。
  • @Fred-ii- 谢谢你的链接。
  • 不客气,雷切尔。

标签: php html mysql


【解决方案1】:

images的数据类型改为Long Blob

【讨论】: