首先,您创建一个MySQL表来存储图像,例如:
create table testblob ( image_id tinyint(3) not null default '0', image_type varchar(25) not null default '', imageblob not null, image_size varchar(25) not null default '', image_ctgy varchar(25) not null default '', image_name varchar(50) not null default '');
然后,您可以将图像写入数据库,如下所示:
$imgData = file_get_contents($filename);$size = getimagesize($filename);mysql_connect("localhost", "$username", "$password");mysql_select_db ("$dbname");// mysqli // $link = mysqli_connect("localhost", $username, $password,$dbname); $sql = sprintf("INSERT INTO testblob (image_type, image, image_size, image_name) VALUES ('%s', '%s', '%d', '%s')", mysql_real_escape_string($size['mime']), mysql_real_escape_string($imgData), $size[3], mysql_real_escape_string($_FILES['userfile']['name']) );mysql_query($sql);
您可以使用以下方法在网页中显示数据库中的图像:
$link = mysql_connect("localhost", "username", "password");mysql_select_db("testblob");$sql = "SELECT image FROM testblob WHERe image_id=0";$result = mysql_query("$sql");header("Content-type: image/jpeg");echo mysql_result($result, 0);mysql_close($link);
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)