努力用php显示blob图像 [英] Struggling to display blob image with php

查看:670
本文介绍了努力用php显示blob图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个简单的网站,我想让用户上传并更改他们的头像。目前我已经能够将图像上传到一个mysql数据库,并以代码形式存储,如下所示:

  //连接到DB,用户ID获取

$图片= $ FILES ['fileToUpload'] ['tmp_name'];
$ fp = fopen($ image,'r');
$ content = fread($ fp,filesize($ image));
$ content = addslashes($ content);
fclose($ fp);

$ sql =UPDATE tbUsers SET profileImage ='。$ content。'WHERE userID =.userID;
$ result = mysql_query($ sql)或die(mysql_error());

当我在上传后从phpmyadmin下载文件时,它们保存为.bin文件,但可以查看一般。我不确定这是否正确。
我的代码显示图像如下:

HTML:

 <?php echo'< img src =showPic.php?q ='。$ _ SESSION ['profile']。'/>'; ?> 

PHP:

  if(!empty($ _ GET ['profile'])&& is_numeric($ _ GET ['profile']))
{
$ con = mysql_connect(localhost , 根, );
$ mysql_select_db(projectDB);
$ sql =SELECT profileImage FROM tbUsers WHERE userID =。 $ _GET [轮廓];
$ result = mysql_query($ sql)或die(mysql_error());

header('Content-type:image / jpeg');
$ row = mysql_fetch_object($ result);
echo $ row ['image_data'];
}

我不确定我是否试图以正确的方式显示图像,任何帮助(更正/替代解决方案)将不胜感激:)

解决方案

您可以这样做:

  if(!empty($ _ GET ['profile'])&& is_numeric($ _ GET ['profile']))
{
$ con = mysql_connect(localhost,root,);
$ mysql_select_db(projectDB);
$ sql =SELECT profileImage FROM tbUsers WHERE userID =。 $ _GET [轮廓];
$ result = mysql_query($ sql)或die(mysql_error());

$ content = mysql_result($ result,0,file_content);
$ name = mysql_result($ result,0,file_name);
$ type = mysql_result($ result,0,file_type);
$ size = mysql_result($ result,0,file_size);
header(Content-type:$ type);
$ b $ echo $ content

}

注:你应该在你的表中保存你的BLOB数据

file_name =用于保存文件名

 $ _ FILES ['file'] ['name'] 

file_type =保存文件类型

$ $ p $ $ $ $ FILES ['file'] ['type']


$ file_size =保存文件大小

pre $ code > $ _ FILES ['file'] ['size']


I am building a simple website, I want to allow the users to upload and change their avatars. At present I have been able to upload images to a mysql database, stored as blobs with the code as follows:

//connected to DB, userID fetched

$image = $FILES['fileToUpload']['tmp_name'];
$fp = fopen($image, 'r');
$content = fread($fp, filesize($image));
$content = addslashes($content);
fclose($fp);

$sql = "UPDATE tbUsers SET profileImage = '".$content."' WHERE userID = ".userID;
$result = mysql_query($sql) or die (mysql_error());

When I download the files from phpmyadmin after upload they are saved as .bin files, but can be viewed normally. I'm not sure if this is correct or not. My code to display the images is as follows:

HTML:

<?php echo '<img src ="showPic.php?q='.$_SESSION['profile'].'"/>'; ?>

PHP:

if (!empty($_GET['profile']) && is_numeric($_GET['profile']))
{
  $con = mysql_connect("localhost", "root", "");
  $mysql_select_db("projectDB");
  $sql = "SELECT profileImage FROM tbUsers WHERE userID = ". $_GET['profile'];
  $result = mysql_query($sql) or die (mysql_error());

  header('Content-type: image/jpeg');
  $row = mysql_fetch_object($result);
  echo $row['image_data'];
}

I am unsure if I am attempting to display the image in the correct way, any help (corrections/alternative solutions) would be greatly appreciated :)

解决方案

You can do this :

if (!empty($_GET['profile']) && is_numeric($_GET['profile']))
{
  $con = mysql_connect("localhost", "root", "");
  $mysql_select_db("projectDB");
  $sql = "SELECT profileImage FROM tbUsers WHERE userID = ". $_GET['profile'];
  $result = mysql_query($sql) or die (mysql_error());

  $content = mysql_result($result,0,"file_content");
  $name = mysql_result($result,0,"file_name");
  $type = mysql_result($result,0,"file_type");
  $size = mysql_result($result,0,"file_size");
  header("Content-type: $type");

  echo $content

}

Note : You should have these column in you table where you save your BLOB data

file_name = for save filename

$_FILES['file']['name']

file_type = for save file type

$_FILES['file']['type']

file_size = for save file size

$_FILES['file']['size']

这篇关于努力用php显示blob图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆