php 连接SQLSERVER2008 并读取16进制图片编码转存本地文件

$fgid = $_GET[‘fgid’];
$connectionInfo = array(“UID”=>”sa”,”PWD”=>”xxx”,”Database”=>”lj”);
$con = sqlsrv_connect(‘127.0.0.1’,$connectionInfo) or die (“err01”);
$query = “SELECT top 10 buiImg from TB_UserInfoImg where buiCode = ‘$fgid'”;
$val=sqlsrv_query($con,$query);
while($row=sqlsrv_fetch_array($val)){
// echo $row[0];
//echo base64_encode($row[0]);
//echo ‘<img src=”data:image/jpg;base64,’.base64_encode($row[0]).'” />’;
$imgdata = ‘data:image/bmp;base64,’.base64_encode($row[0]);
//echo $imgdata.'<br>’;
//匹配出图片的格式

//匹配出图片的格式
if (preg_match(‘/^(data:\s*image\/(\w+);base64,)/’, $imgdata, $result)){
$type = $result[2];
$new_file = “uploads/userup/”;
if(!file_exists($new_file))
{
//检查是否有该文件夹,如果没有就创建,并给予最高权限
mkdir($new_file, 0700);
}
$new_file = $new_file.$fgid.”.{$type}”;
if (file_put_contents($new_file, base64_decode(str_replace($result[1], ”,$imgdata)))){
echo ‘/’. $new_file;
}else{
echo ‘/uploads/userup/100001.bmp’;
}
}
}

给个演示输出的PHP:index

作者: sinma

sinma 存在于自然,存在于你身边。

发表评论