php上传图片到指定位置路径保存到数据库的具体实现
程序员文章站
2023-11-16 22:33:16
1.conn.php 复制代码 代码如下: $host="localhost"; //数据库服务器名称 $user="root"; //用户名 $pwd="17...
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"<script>window.alert('对不起!你输入的信息不完整!');history.back()</script>";
$sql="insert into upload(title,pic) values('$title','$pic')";
$result=mysql_query($sql,$conn);
//echo"<script>window.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>
<td><input name="title" type="text" id="title"></td>
</tr>
<tr>
<td>文件: </td>
<td><label>
<input name="file" type="file" value="浏览" >
<input type="hidden" name="max_file_size" value="2000000">
</label></td>
</tr>
<tr>
<td> </td>
<td><input 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>您的文件已经上传完毕 上传图片预览: </center><br><center><img src='$uploadfile'></center>";
echo "<br><center><a href='upload.htm'>继续上传</a></center>";
}
}
}
?>
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 ', 'ag ', '1.html'),
(2, '你好2', '你好,新闻内容2', '2.html'),
(3, '', '', '3.html'),
(4, 'ga ', 'ag ', '4.html'),
(5, 'ag ', 'ag ', '1.html'),
(6, 'ag ', 'ag ', '2.html'),
(7, 'ag ', 'ag ', '3.html');
-- --------------------------------------------------------
--
-- 表的结构 `upload`
--
create table `upload` (
`uploadid` int(11) unsigned not null auto_increment,
`title` varchar(50) not null,
`pic` varchar(50) not null,
primary key (`uploadid`)
) engine=innodb default charset=gb2312 auto_increment=10 ;
--
-- 导出表中的数据 `upload`
--
insert into `upload` (`uploadid`, `title`, `pic`) values
(1, 'ga ', 'upfiles/cr-xpvdk12dsi.jpg'),
(2, 'ga ', 'upfiles/cr-hkc6fbfatw.jpg'),
(3, '你好', 'upfiles/cr-e8ohxk6kxb.jpg'),
(4, '', ''),
(5, '', ''),
(6, '', 'upfiles/cr-92xaug6jhu.jpg'),
(7, '', 'upfiles/cr-a7niazlhfw.jpg'),
(8, '帅哥', 'upfiles/cr-v9ary46jos.jpg'),
(9, '爱国', 'upfiles/cr-xyajendf1k.jpg');
复制代码 代码如下:
<?
$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"<script>window.alert('对不起!你输入的信息不完整!');history.back()</script>";
$sql="insert into upload(title,pic) values('$title','$pic')";
$result=mysql_query($sql,$conn);
//echo"<script>window.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>
<td><input name="title" type="text" id="title"></td>
</tr>
<tr>
<td>文件: </td>
<td><label>
<input name="file" type="file" value="浏览" >
<input type="hidden" name="max_file_size" value="2000000">
</label></td>
</tr>
<tr>
<td> </td>
<td><input 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>您的文件已经上传完毕 上传图片预览: </center><br><center><img src='$uploadfile'></center>";
echo "<br><center><a href='upload.htm'>继续上传</a></center>";
}
}
}
?>
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 ', 'ag ', '1.html'),
(2, '你好2', '你好,新闻内容2', '2.html'),
(3, '', '', '3.html'),
(4, 'ga ', 'ag ', '4.html'),
(5, 'ag ', 'ag ', '1.html'),
(6, 'ag ', 'ag ', '2.html'),
(7, 'ag ', 'ag ', '3.html');
-- --------------------------------------------------------
--
-- 表的结构 `upload`
--
create table `upload` (
`uploadid` int(11) unsigned not null auto_increment,
`title` varchar(50) not null,
`pic` varchar(50) not null,
primary key (`uploadid`)
) engine=innodb default charset=gb2312 auto_increment=10 ;
--
-- 导出表中的数据 `upload`
--
insert into `upload` (`uploadid`, `title`, `pic`) values
(1, 'ga ', 'upfiles/cr-xpvdk12dsi.jpg'),
(2, 'ga ', 'upfiles/cr-hkc6fbfatw.jpg'),
(3, '你好', 'upfiles/cr-e8ohxk6kxb.jpg'),
(4, '', ''),
(5, '', ''),
(6, '', 'upfiles/cr-92xaug6jhu.jpg'),
(7, '', 'upfiles/cr-a7niazlhfw.jpg'),
(8, '帅哥', 'upfiles/cr-v9ary46jos.jpg'),
(9, '爱国', 'upfiles/cr-xyajendf1k.jpg');