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

2015-01-24信息快讯网

本文为大家介绍下php上传图片到指定位置路径保存到数据库的具体实现,感兴趣的朋友不要错过

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');
Thinkphp实现MySQL读写分离操作示例
PHP生成等比缩略图类和自定义函数分享
PHP使用DOMDocument类生成HTML实例(包含常见标签元素)
PHP内置过滤器FILTER使用实例
PHP生成图片验证码、点击切换实例
Discuz批量替换帖子内容的方法(使用SQL更新数据库)
美图秀秀web开放平台--PHP流式上传和表单上传示例分享
destoon会员注册提示“数据校验失败(2)”解决方法
destoon二次开发常用数据库操作
ThinkPHP3.1数据CURD操作快速入门
ThinkPHP3.1新特性之多数据库操作更加完善
php+ajax实现图片文件上传功能实例
Yii结合CKEditor实现图片上传功能
Codeigniter上传图片出现“You did not select a file to upload”错误解决办法
Codeigniter实现多文件上传并创建多个缩略图
新浪SAE云平台下使用codeigniter的数据库配置
Codeigniter操作数据库表的优化写法总结
php实现执行某一操作时弹出确认、取消对话框
php-perl哈希算法实现(times33哈希算法)
php实现在线生成条形码示例分享(条形码生成器)
浅谈PHP变量作用域以及地址引用问题
一个好用的PHP验证码类实例分享
PHP连接SQLServer2005方法及代码
zend framework文件上传功能实例代码
phpexcel导入excel数据使用方法实例
php jquery 多文件上传简单实例
php使用curl发送json格式数据实例
php文件上传的例子及参数详解
PHP数据库链接类(PDO+Access)实例分享
PHP上传文件时文件过大$_FILES为空的解决方法
利用phpExcel实现Excel数据的导入导出(全步骤详细解析)
php导出word格式数据的代码实例
限制ckeditor上传图片文件大小的方法
三种php连接access数据库方法
php批量更改数据库表前缀实现方法
php中读写文件与读写数据库的效率比较分享
php中将数组转成字符串并保存到数据库中的函数代码
©2014-2024 dbsqp.com