简体   繁体   English

如何上传图片并存储在数据库中

[英]how to upload picture and store in database

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

$image = mysql_real_escape_string($image);


mysql_query("UPDATE ngc set pic='" . $image "' WHERE username='" . $_SESSION["username"] . "'");


<form method="post" action="" enctype="multipart/form-data">

Upload Image :<input type="file" name="image" id="file">
<br><input type="submit" name="submit" value="Update!" class="btnSubmit">

</form>

i want to upload image to database.. 我想将图像上传到数据库。

Read this very nice example here: http://www.mysqltutorial.org/php-mysql-blob/ Also see this image gallery example here: http://www.anyexample.com/programming/php/php_mysql_example__image_gallery_(blob_storage).xml 在此处阅读这个非常不错的示例: http : //www.mysqltutorial.org/php-mysql-blob/在此处也请参见此图像库示例: http : //www.anyexample.com/programming/php/php_mysql_example__image_gallery_(blob_storage)。 XML文件

BLOBs are data types in MySql database which can help you store image files in database directly. BLOB是MySql数据库中的数据类型,可以帮助您将图像文件直接存储在数据库中。

Although a better way would be to store the file on the disk and store the path of that variable in the database. 尽管更好的方法是将文件存储在磁盘上并将该变量的路径存储在数据库中。

get the Content of the file and save in database; 获取文件内容并保存在数据库中;

$image = addslashes(file_get_contents($_FILES['image']['tmp_name']));

Mysql: MySQL的:

$sql = "INSERT INTO `product_images` (`id`, `image`) VALUES ('1', '{$image}')";

首先,如果要将整个图像保存到数据库中,必须将pic属性类型设置为BLOB,这里有两个选项,要么将整个图像保存到数据库中,要么将图像上传到数据库后保存名称。指定的文件夹,每个文件夹具有唯一的名称,因此您可以按名称将图像检索到该目录中。

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

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