我正在尝试为会员资料上传图片并使用 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;
?>