php上传图片到指定位置路径保存到数据库的具体实现

前端之家收集整理的这篇文章主要介绍了php上传图片到指定位置路径保存到数据库的具体实现前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

1.conn.PHP
<div class="codetitle"><a style="CURSOR: pointer" data="11942" class="copybut" id="copybut11942" onclick="doCopy('code11942')"> 代码如下:

<div class="codebody" id="code11942">
<?
$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 "
服务器连接失败!
请刷新后重试。
";
return true;
}
$databasename="database";//数据库名称 do
{
$con=MysqL_select_db($databasename,$conn);
}while(!$con); if ($con==FALSE)
{
echo "
打开数据库失败!
请刷新后重试。
";
return true;
} ?>

2.upload.PHP
<div class="codetitle"><a style="CURSOR: pointer" data="3656" class="copybut" id="copybut3656" onclick="doCopy('code3656')"> 代码如下:
<div class="codebody" id="code3656">
<?PHP
if ($_GET['action'] == "save"){
include_once('conn.PHP');
include_once('uploadclass.PHP');
$title=$_POST['title'];
$pic=$uploadfile;
if($title == "")
echo"";
$sql="insert into upload(title,pic) values('$title','$pic')";
$result=MysqL_query($sql,$conn);
//echo"";
}
?>


<a href="https://www.jb51.cc/tag/wenjian/" target="_blank" class="keywords">文件</a><a href="https://www.jb51.cc/tag/shangchuan/" target="_blank" class="keywords">上传</a>实例


<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 height="16"> <table width="48%" height="93" border="0" cellpadding="0" cellspacing="0">
<tr>
<td>标题:</td>
<td><input name="title" type="text" id="title"></td>
</tr>
<tr>
<td>文件: </td>
<td></td>
</tr>
<tr>
<td></td>
<td><input type="submit" value="上 传" name="upload"></td>
</tr>
</table></td>
</tr>
</table>



3.uploadclass.PHP
<div class="codetitle"><a style="CURSOR: pointer" data="81630" class="copybut" id="copybut81630" onclick="doCopy('code81630')"> 代码如下:
<div class="codebody" id="code81630">
<?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,"
";
}
//生成目标文件文件
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 "
您的文件已经上传完毕 上传图片预览:

";
echo "
继续上传
";
}
} }
?>

4.数据库文件
-- PHPMyAdmin sql Dump
-- version 2.9.1.1
-- http://www.PHPmyadmin.net
--
-- 主机: localhost
-- 生成日期: 2009 年 07 月 30 日 21:43
-- 服务器版本: 5.0.11
-- PHP 版本: 5.1.1
--
-- 数据库: database
-- -- -------------------------------------------------------- --
-- 表的结构 news
-- CREATE TABLE news (
id int(11) unsigned NOT NULL auto_increment,
title varchar(50) NOT NULL,
content varchar(50) NOT NULL,
path text NOT NULL,
PRIMARY KEY (id)
) ENGINE=InnoDB DEFAULT CHARSET=gb2312 AUTO_INCREMENT=8 ; --
-- 导出表中的数据 news
-- INSERT INTO news (id,title,content,path) VALUES
(1,'ag ','1.html'),
(2,'你好2','你好,新闻内容2','2.html'),
(3,'','3.html'),
(4,'ga ','4.html'),
(5,
(6,
(7,'3.html'); -- -------------------------------------------------------- --
-- 表的结构 upload
-- CREATE TABLE upload (
uploadid int(11) unsigned NOT NULL auto_increment,
pic varchar(50) NOT NULL,
PRIMARY KEY (uploadid)
) ENGINE=InnoDB DEFAULT CHARSET=gb2312 AUTO_INCREMENT=10 ; --
-- 导出表中的数据 upload
-- INSERT INTO upload (uploadid,pic) VALUES
(1,'upfiles/CR-xpvDk12dsI.jpg'),'upfiles/CR-HkC6fBfAtW.jpg'),'你好','upfiles/CR-E8Ohxk6KxB.jpg'),''),'upfiles/CR-92xaug6jhu.jpg'),'upfiles/CR-a7niAZlhfW.jpg'),
(8,'帅哥','upfiles/CR-v9Ary46JoS.jpg'),
(9,'爱国','upfiles/CR-XyaJEndF1K.jpg');

原文链接:https://www.f2er.com/php/25329.html
上传图片保存到数据库指定路径

猜你在找的PHP相关文章