简体   繁体   English

将图像和信息存储到数据库中,并使用MySql Query检索以使用PHP显示

[英]Storing image and information into database and retrieve using MySql Query to display using PHP

I want to store some information in the form and the image into database. 我想将一些信息以表格和图像的形式存储到数据库中。

For Example: 例如:

    <form name="form1" enctype="multipart/form-data" method="post" action="do_send.php">
  <label>
  <input type="text" name="name" id="name" />
  </label>

<label>

  <input type="text" name="age" id="age" />
  </label>


  <label>
  <input type="email" name="email" id="email" />
  </label>

  <label>
  <input type="file" name="my_file" />
  </label>
  <label>

  <input type="submit" name="button" value="Submit" />
  </label>
</form>

I want to store all this into database.I can store the data in the database but don't know how to store image. 我想将所有这些存储到数据库中。我可以将数据存储在数据库中,但不知道如何存储图像。 Kindly tell me how can i do this?? 请告诉我我该怎么做?

Also i want to display the data and image on another page using MySql query. 我也想使用MySql查询在另一页上显示数据和图像。

How can i do both these tasks. 我该如何完成这两项任务。 I am limited knowledge as i am a starter. 我是个初学者,所以知识有限。 Please guide me with code and a little bit of explanation. 请用代码和一些解释指导我。

use move_uploaded_file() function 使用move_uploaded_file()函数

create new folder in php project directory to store images. 在php project目录中创建新文件夹以存储图像。

only store path of image in database. 仅将图像的路径存储在数据库中。

$fileName = $_FILES["uploaded_file"]["name"]; $fileTmpLoc = $_FILES["uploaded_file"]["tmp_name"]; $pathAndName = "uploads/".$fileName; $moveResult = move_uploaded_file($fileTmpLoc, $pathAndName); store $pathAndName variable value in database. 将$ pathAndName变量值存储在数据库中。 "uploads" is a folder name. “上载”是文件夹名称。

Use buffering + imagepng . 使用缓冲 + imagepng

In do_send.php you can try with something like this : do_send.php您可以尝试执行以下操作:

$image = imagecreatefromstring($my_file);

// start buffering
ob_start();
imagepng($image);
$contents = ob_get_contents();
ob_end_clean();

$str_base64_img = base64_encode($contents);

imagedestroy($image);

In my opinion, it's not a good pratice to directly store image datas in db and base64_encode enlarge to 33% your datas. 在我看来,将图像数据直接存储在db中并且base64_encode放大到您的数据的33%并不是一个好习惯。

You should be used an hosting image system and store the path to the image in your db. 您应该使用托管映像系统,并将映像的路径存储在数据库中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM