网创优客建站品牌官网
为成都网站建设公司企业提供高品质网站建设
热线:028-86922220
成都专业网站建设公司

定制建站费用3500元

符合中小企业对网站设计、功能常规化式的企业展示型网站建设

成都品牌网站建设

品牌网站建设费用6000元

本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...

成都商城网站建设

商城网站建设费用8000元

商城网站建设因基本功能的需求不同费用上面也有很大的差别...

成都微信网站建设

手机微信网站建站3000元

手机微信网站开发、微信官网、微信商城网站...

建站知识

当前位置:首页 > 建站知识

php把数据放在图片里面 php图片路径上传到数据库

php中如何将图片储存在数据库里

两种方法:

创新互联公司基于成都重庆香港及美国等地区分布式IDC机房数据中心构建的电信大带宽,联通大带宽,移动大带宽,多线BGP大带宽租用,是为众多客户提供专业四川雅安服务器托管报价,主机托管价格性价比高,为金融证券行业服务器托管,ai人工智能服务器托管提供bgp线路100M独享,G口带宽及机柜租用的专业成都idc公司。

一:将图片上传至指定目录,在数据库中保存文件名和文件路径。

二:将图片文件读入字符串,将字符串保存到数据库,不推荐(没那么长的字段长度支持)。

PHP怎么把图片数据保存为jpg图片到服务器目录

第一步:通过$_FILES获取文件信息。

第二步:指定新文件名称以及路径,并赋值给一个变量。

第三步:通过move_uploaded_file上传文件。

第四步:上传成功后,将数值存入数据库服务器目录即可。

代码如下

1.conn.php

$host="localhost"; //数据库服务器名称 

$user="root"; //用户名 

$pwd="1721"; //密码 

$conn=mysql_connect($host,$user,$pwd); 

mysql_query("SET 

character_set_connection=gb2312, 

character_set_results=gb2312, 

character_set_client=binary",$conn); 

if ($conn==FALSE) 

echo "center服务器连接失败!br请刷新后重试。/center"; 

return true; 

$databasename="database";//数据库名称 

do 

$con=mysql_select_db($databasename,$conn); 

}while(!$con); 

if ($con==FALSE) 

echo "center打开数据库失败!br请刷新后重试。/center"; 

return true; 

?

2.upload.php

?php 

if ($_GET['action'] == "save"){ 

include_once('conn.php'); 

include_once('uploadclass.php'); 

$title=$_POST['title']; 

$pic=$uploadfile; 

if($title == "") 

echo"Scriptwindow.alert('对不起!你输入的信息不完整!');history.back()/Script"; 

$sql="insert into upload(title,pic) values('$title','$pic')"; 

$result=mysql_query($sql,$conn); 

//echo"Scriptwindow.alert('信息添加成功');location.href='upload.php'/Script"; 

html 

head 

title文件上传实例/title 

/head 

body 

form method="post" action="?action=save" enctype="multipart/form-data" 

table border=0 cellspacing=0 cellpadding=0 align=center width="100%" 

tr 

td width=55 height=20 align="center" /TD 

td height="16" 

table width="48%" height="93" border="0" cellpadding="0" cellspacing="0" 

tr 

td标题:/td 

tdinput name="title" type="text" id="title"/td 

/tr 

tr 

td文件: /td 

tdlabel 

input name="file" type="file" value="浏览"  

input type="hidden" name="MAX_FILE_SIZE" value="2000000" 

/label/td 

/tr 

tr 

td /td 

tdinput type="submit" value="上 传" name="upload"/td 

/tr 

/table/td 

/tr 

/table 

/form 

/body 

/html

3.uploadclass.php

?php 

$uploaddir = "upfiles/";//设置文件保存目录 注意包含/ 

$type=array("jpg","gif","bmp","jpeg","png");//设置允许上传文件的类型 

$patch="upload/";//程序所在路径 

//获取文件后缀名函数 

function fileext($filename) 

return substr(strrchr($filename, '.'), 1); 

//生成随机文件名函数 

function random($length) 

$hash = 'CR-'; 

$chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz'; 

$max = strlen($chars) - 1; 

mt_srand((double)microtime() * 1000000); 

for($i = 0; $i  $length; $i++) 

$hash .= $chars[mt_rand(0, $max)]; 

return $hash; 

$a=strtolower(fileext($_FILES['file']['name'])); 

//判断文件类型 

if(!in_array(strtolower(fileext($_FILES['file']['name'])),$type)) 

$text=implode(",",$type); 

echo "您只能上传以下类型文件: ",$text,"br"; 

//生成目标文件的文件名 

else{ 

$filename=explode(".",$_FILES['file']['name']); 

do 

$filename[0]=random(10); //设置随机数长度 

$name=implode(".",$filename); 

//$name1=$name.".Mcncc"; 

$uploadfile=$uploaddir.$name; 

while(file_exists($uploadfile)); 

if (move_uploaded_file($_FILES['file']['tmp_name'],$uploadfile)) 

if(is_uploaded_file($_FILES['file']['tmp_name'])) 

echo "上传失败!"; 

else 

{//输出图片预览 

echo "center您的文件已经上传完毕 上传图片预览: /centerbrcenterimg src='$uploadfile'/center"; 

echo "brcentera href='upload.htm'继续上传/a/center"; 

?

怎样把图片插入到数据库中 php

保存图片到数据库做什么?保存到本地使用起来也方便,真要保存通过base64字符串保存。

?php

header('Content-type:text/html;charset=utf-8');

//读取图片文件,转换成base64编码格式

$image_file = './image123.jpg';

$image_info = getimagesize($image_file);

$base64_image_content = "data:{$image_info['mime']};base64," . chunk_split(base64_encode(file_get_contents($image_file)));

// $base64_image_content 输入到数据库

//保存base64字符串为图片

//匹配出图片的格式

if (preg_match('/^(data:\s*image\/(\w+);base64,)/', $base64_image_content, $result)){

$type = $result[2];

$new_file = "./test.{$type}";

if (file_put_contents($new_file, base64_decode(str_replace($result[1], '', $base64_image_content)))){

echo '新文件保存成功:', $new_file;

}

}

?

img src="?php echo $base64_image_content;?" /


当前名称:php把数据放在图片里面 php图片路径上传到数据库
路径分享:http://bjjierui.cn/article/dosejph.html

其他资讯