如何使用php插入和检索图像与数据库

问题描述 投票:1回答:6

即时尝试上传成员个人资料的图像,并使用PHP将其存储在数据库中,然后检索它,但它不适用于我

这是我试图插入图像:

<html>
<head>
<title> Upload an image </title>
</head>
<body>
<form action="index.php" method="POST" enctype="multipart/from/data">
  File:
  <input type="file" name="image" > <input type="submit" value="upload">
</form>
</body>
</html>
<?php

  mysql_connect("localhost", "root","") or die ("could not connect to the server");
  mysql_select_db("project") or die ("that database could not be found");

  $file = $_FILES['image']['tmp_name'];

  if (!isset($file))
     echo "please select file";
  else
  {

     $image= addslashes(file_get_contents($_FILES['image']['tmp_name']));
     $image_name = addslashes($_FILES['image']['name']);
     $image_size = getimagesize($_FILES['image']['tmp_name']);

     if($image_size == FALSE)
        echo "that not image ";

     else
     {

         if (!$insert= mysql_query("INSERT INTO user_info (image) VALUES ('$image')"))
             echo "Problem";

         else
         {

             echo "image: <p /> your image <p /><img src='view.php?id="id"'>";
         }
     }


 }

这用于检索图像

<?php

   // do some validation here to ensure id is safe
   mysql_connect("localhost", "root","") or die ("could not connect to the server");
   mysql_select_db("project") or die ("that database could not be found");
   $id = addslashes($_REQUEST['id']);

   $image = mysql_query("SELECT * FROM user_info WHERE id='$id'");
   $image = mysql_fetch_assoc($image);
   $image = $image['image'];

   header("Content-type: image/jpeg");
   echo $image;
?>
php mysql html5 upload
6个回答
2
投票

我想你应该重新思考你在做什么。

为什么要将文件存储在mysql数据库中?我的意思是什么意思呢?

文件系统是用于存储文件的专家数据库。因此,最好不要将文件保存在普通数据库中,而是保存在文件系统中。

图像可以用目录中的数字命名(0102003.gif),并且可以通过mysql记录中的文件名进行寻址。

如果你真的需要在数据库中存储文件,那么mysql就不是那个工具。

看看mongoDB。

PS:不推荐使用mysql接口,请使用mysqli或PDO。


1
投票

试试这段代码它会对你有所帮助。

<?php
    mysql_connect("localhost", "root","") or die ("could not connect to the server");
    mysql_select_db("database1") or die ("that database could not be found");

    $file = $_FILES['image']['tmp_name'];

    $image= addslashes(file_get_contents($_FILES['image']['tmp_name']));
    $image_name = addslashes($_FILES['image']['name']);
    $image_size = getimagesize($_FILES['image']['tmp_name']);

    mysql_query("INSERT INTO table1 (id,image) VALUES ('1','{$image}')");        
?>

0
投票

要在php网站上存储图像,只需将文件放在目录中并将文件名保存在mySQL数据库中即可。祝好运!



0
投票

您需要在表单声明中使用enctype = multipart / form-data。并通过$ _FILES变量而不是$ _POST变量访问该文件。喜欢:

<form action="testimage1.php" method="post" enctype="multipart/form-data"> <input name="uploadimage" type="file" /> </form> <?php $filename = $_FILES['uploadimage']['tmp_name']; ?>


0
投票

您可以通过两种不同的方式插入和检索图像:

方法1:

将整个图像存储在longblob类型的数据库中,并从数据库中检索它。

插入图片:

<?php
//Get uploaded file data
$img_tmp_name    = $_FILES['image']['tmp_name'];
$img_name        = $_FILES['image']['name'];
$img_size        = $_FILES['image']['size'];
$img_type        = $_FILES['image']['type'];
$img_error       = $_FILES['image']['error'];
//Processed uploaded img data
if($img_error > 0)
{
    die('Upload error or No img uploaded');
}
$handle = fopen($img_tmp_name, "r");
$content = fread($handle, $img_size);
fclose($handle);
$encoded_content = addslashes($content);

$query="insert into user_info (img_content,img_type) values('".$encoded_content."','".$img_type."')";
$ex=mysqli_query($conn,$query);
if($ex){
    echo "image uploaded";
}
else{
    echo "image not uploaded<br/>".mysqli_error($conn);
}
?>

检索图片:

您可以通过2种方法检索图像,首先使用处理程序PHP脚本返回图像数据,第二种方法如下:

<?php
    $query="select * from user_info WHERE id='".$id."'";
    $ex=mysqli_query($conn,$query);
    $row=mysqli_fetch_array($ex)
?>              
<img src="<?php echo "data:'".$row['img_type']."';base64,".base64_encode($row['img_content']); ?>" />

但是如果图像尺寸很大,则不推荐使用方法1。

方法2:

在此方法中,我们仅将图像的路径存储在数据库中。我们将图像存储在目录中。

插入图片:

<?php
    $target_dir = "image/";
    $target_file = $target_dir . basename($_FILES["image"]["name"]);
    move_uploaded_file($_FILES["image"]["tmp_name"], $target_file);

    $query="insert into user_info (img_path) values('".$target_file."')";
    $ex=mysqli_query($conn,$query);
    if($ex){
        echo "image uploaded";
    }
    else{
        echo "image not uploaded<br/>".mysqli_error($conn);
    }
?>

检索图片:

<?php
    $query="select * from user_info WHERE id='".$id."'";
    $ex=mysqli_query($conn,$query);
    $row=mysqli_fetch_array($ex)
?>
<img src="<?php echo $row['img_path']; ?>" alt=""/>
© www.soinside.com 2019 - 2024. All rights reserved.