php 如何使用PHP代码将图像上传到MySQL数据库
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17717506/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to upload images into MySQL database using PHP code
提问by Taha Kirmani
I am trying to save images in my database from HTML form. I have written PHP code to accomplish this task. The program is not generating any error message, but also not inserting image data in MySQL database. Kindly check it. Here i am sharing a excerpt from my code.
我正在尝试从 HTML 表单将图像保存在我的数据库中。我已经编写了 PHP 代码来完成这个任务。该程序没有生成任何错误消息,也没有在 MySQL 数据库中插入图像数据。请检查一下。在这里,我分享了我的代码的摘录。
/*-------------------
IMAGE QUERY
---------------*/
$file =$_FILES['image']['tmp_name'];
if(!isset($file))
{
echo 'Please select an Image';
}
else
{
$image_check = getimagesize($_FILES['image']['tmp_name']);
if($image_check==false)
{
echo 'Not a Valid Image';
}
else
{
$image = file_get_contents ($_FILES['image']['tmp_name']);
$image_name = $_FILES['image']['name'];
if ($image_query = mysql_query ("insert into product_images values (1,'$image_name',$image )"))
{
echo $current_id;
//echo 'Successfull';
}
else
{
echo mysql_error();
}
}
}
/*-----------------
IMAGE QUERY END
---------------------*/
<form action='insert_product.php' method='POST' enctype='multipart/form-data' ></br>
File : <input type='file' name= 'image' >
</form>
Error Message You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1
错误消息 您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,以了解要在第 1 行的 '' 附近使用的正确语法
回答by Wiktor Mociun
Firstly, you should check if your image column is BLOB type!
首先,您应该检查您的图像列是否为 BLOB 类型!
I don't know anything about your SQL table, but if I'll try to make my own as an example.
我对您的 SQL 表一无所知,但是如果我尝试以自己的方式作为示例。
We got fields id
(int), image
(blob) and image_name
(varchar(64)).
我们得到了字段id
(int)、image
(blob) 和image_name
(varchar(64))。
So the code should look like this (assume ID is always '1' and let's use this mysql_query):
所以代码应该是这样的(假设 ID 总是“1”,让我们使用这个 mysql_query):
$image = addslashes(file_get_contents($_FILES['image']['tmp_name'])); //SQL Injection defence!
$image_name = addslashes($_FILES['image']['name']);
$sql = "INSERT INTO `product_images` (`id`, `image`, `image_name`) VALUES ('1', '{$image}', '{$image_name}')";
if (!mysql_query($sql)) { // Error handling
echo "Something went wrong! :(";
}
You are doing it wrong in many ways. Don't use mysql functions - they are deprecated! Use PDOor MySQLi. You should also think about storing files locations on disk. Using MySQL for storing images is thought to be Bad Idea?. Handling SQL table with big data like images can be problematic.
你在很多方面都做错了。不要使用 mysql 函数 - 它们已被弃用!使用PDO或MySQLi。您还应该考虑在磁盘上存储文件位置。使用 MySQL 存储图像被认为是坏主意?。处理带有大数据(如图像)的 SQL 表可能会出现问题。
Also your HTML form is out of standards. It should look like this:
此外,您的 HTML 表单不符合标准。它应该是这样的:
<form action="insert_product.php" method="POST" enctype="multipart/form-data">
<label>File: </label><input type="file" name="image" />
<input type="submit" />
</form>
Sidenote:
边注:
When dealing with files and storing them as a BLOB, the data must be escaped using mysql_real_escape_string()
, otherwise it will result in a syntax error.
在处理文件并将它们存储为 BLOB 时,必须使用 对数据进行转义mysql_real_escape_string()
,否则会导致语法错误。
回答by Sean
Just few more details:
更多细节:
- Add mysql field
- 添加mysql字段
`image` blob
`image` blob
- Get data from image
- 从图像中获取数据
$image = addslashes(file_get_contents($_FILES['image']['tmp_name']));
$image = addslashes(file_get_contents($_FILES['image']['tmp_name']));
- Insert image data into db
- 将图像数据插入数据库
$sql = "INSERT INTO `product_images` (`id`, `image`) VALUES ('1', '{$image}')";
$sql = "INSERT INTO `product_images` (`id`, `image`) VALUES ('1', '{$image}')";
- Show image to the web
- 向网络显示图像
<img src="data:image/png;base64,'.base64_encode($row['image']).'">
<img src="data:image/png;base64,'.base64_encode($row['image']).'">
- End
- 结尾
回答by Mayur Gudi
This is the perfect code for uploading and displaying image through MySQL database.
这是通过 MySQL 数据库上传和显示图像的完美代码。
<html>
<body>
<form method="post" enctype="multipart/form-data">
<input type="file" name="image"/>
<input type="submit" name="submit" value="Upload"/>
</form>
<?php
if(isset($_POST['submit']))
{
if(getimagesize($_FILES['image']['tmp_name'])==FALSE)
{
echo " error ";
}
else
{
$image = $_FILES['image']['tmp_name'];
$image = addslashes(file_get_contents($image));
saveimage($image);
}
}
function saveimage($image)
{
$dbcon=mysqli_connect('localhost','root','','dbname');
$qry="insert into tablename (name) values ('$image')";
$result=mysqli_query($dbcon,$qry);
if($result)
{
echo " <br/>Image uploaded.";
header('location:urlofpage.php');
}
else
{
echo " error ";
}
}
?>
</body>
</html>