php 如何使用php在数据库中插入和检索图像
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16382672/
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 insert and retrieve images to and from database using php
提问by fatimah
im trying to upload an image for a member profile and store it in a database using php then retrieve it but its does not work with me
我正在尝试为成员个人资料上传图像并使用 php 将其存储在数据库中,然后检索它,但它对我不起作用
this is what im trying for inserting the image :
这就是我试图插入图像的内容:
<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"'>";
}
}
}
and this for retrieving the image
这是用于检索图像
<?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;
?>
回答by Ihsan
I guess you should re-think what you are doing.
我想你应该重新考虑你在做什么。
Why do you store files in a mysql database? I mean what is the point in that?
为什么要将文件存储在 mysql 数据库中?我的意思是这有什么意义?
File system is an expert database for storing files. So better do not keep your files in a normal database but in the file system.
文件系统是存储文件的专家数据库。所以最好不要将文件保存在普通数据库中,而是保存在文件系统中。
Images can be named with numbers in directories (0102003.gif) and can be addressed via the filenames in your mysql records.
图像可以在目录 (0102003.gif) 中用数字命名,并且可以通过 mysql 记录中的文件名进行寻址。
If you reallyneed file storage in a database, mysql is not the tool for that.
如果您确实需要在数据库中存储文件,那么 mysql 不是用于此的工具。
Have a look at mongoDB.
看看 mongoDB。
PS: mysql interface is deprecated, please use mysqli or PDO.
PS:mysql接口已弃用,请使用mysqli或PDO。
回答by Shenal
Try This code it will help you.
试试这个代码它会帮助你。
<?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}')");
?>
回答by fatimah
You need enctype=multipart/form-data in your form declaration. And access the file through the $_FILES variable instead of the $_POST variable. Like:
您需要在表单声明中使用 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']; ?>
回答by Aashish
There are 2 different ways by which you can insert and retrieve images:
您可以通过两种不同的方式插入和检索图像:
Method 1:
方法一:
Store entire image in the database itself in longblob
type and retrieve it from the database.
将整个图像按longblob
类型存储在数据库本身中,并从数据库中检索它。
Insert image:
插入图片:
<?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);
}
?>
Retrieve image:
检索图像:
You can retrieve an image by 2 methods, first by using a handler PHP script to return the image data and the second one as follows:
您可以通过两种方法检索图像,第一种是使用处理程序 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']); ?>" />
But method 1 is not a recommended way if the image size will be large.
但是如果图像尺寸很大,则方法 1 不是推荐的方法。
Method 2:
方法二:
In this method, we only store the path of the image in the database. We store the images in a directory.
在这种方法中,我们只将图像的路径存储在数据库中。我们将图像存储在一个目录中。
Insert image:
插入图片:
<?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);
}
?>
Retrieve image:
检索图像:
<?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=""/>
回答by JC Ricaro
To store images on a php website, you just have to put the files in a directory and save the filenames on the mySQL database. Goodluck!
要将图像存储在 php 网站上,您只需将文件放在一个目录中并将文件名保存在 mySQL 数据库中。祝你好运!
回答by JC Ricaro
i hope this like use full to you:-
我希望这对你有用:-