php上傳圖片到指定位置路徑保存到數(shù)據(jù)庫(kù)的具體實(shí)現(xiàn)
更新時(shí)間:2013年12月30日 16:53:06 作者:
本文為大家介紹下php上傳圖片到指定位置路徑保存到數(shù)據(jù)庫(kù)的具體實(shí)現(xiàn),感興趣的朋友不要錯(cuò)過(guò)
1.conn.php
<?
$host="localhost"; //數(shù)據(jù)庫(kù)服務(wù)器名稱
$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>服務(wù)器連接失??!<br>請(qǐng)刷新后重試。</center>";
return true;
}
$databasename="database";//數(shù)據(jù)庫(kù)名稱
do
{
$con=mysql_select_db($databasename,$conn);
}while(!$con);
if ($con==FALSE)
{
echo "<center>打開(kāi)數(shù)據(jù)庫(kù)失??!<br>請(qǐng)刷新后重試。</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('對(duì)不起!你輸入的信息不完整!');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>文件上傳實(shí)例</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>標(biāo)題:</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/";//設(shè)置文件保存目錄 注意包含/
$type=array("jpg","gif","bmp","jpeg","png");//設(shè)置允許上傳文件的類型
$patch="upload/";//程序所在路徑
//獲取文件后綴名函數(shù)
function fileext($filename)
{
return substr(strrchr($filename, '.'), 1);
}
//生成隨機(jī)文件名函數(shù)
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>";
}
//生成目標(biāo)文件的文件名
else{
$filename=explode(".",$_FILES['file']['name']);
do
{
$filename[0]=random(10); //設(shè)置隨機(jī)數(shù)長(zhǎng)度
$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
{//輸出圖片預(yù)覽
echo "<center>您的文件已經(jīng)上傳完畢 上傳圖片預(yù)覽: </center><br><center><img src='$uploadfile'></center>";
echo "<br><center><a href='upload.htm'>繼續(xù)上傳</a></center>";
}
}
}
?>
4.數(shù)據(jù)庫(kù)文件
-- phpMyAdmin SQL Dump
-- version 2.9.1.1
-- http://www.phpmyadmin.net
--
-- 主機(jī): localhost
-- 生成日期: 2009 年 07 月 30 日 21:43
-- 服務(wù)器版本: 5.0.11
-- PHP 版本: 5.1.1
--
-- 數(shù)據(jù)庫(kù): `database`
--
-- --------------------------------------------------------
--
-- 表的結(jié)構(gòu) `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 ;
--
-- 導(dǎo)出表中的數(shù)據(jù) `news`
--
INSERT INTO `news` (`id`, `title`, `content`, `path`) VALUES
(1, 'ag ', 'ag ', '1.html'),
(2, '你好2', '你好,新聞內(nèi)容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');
-- --------------------------------------------------------
--
-- 表的結(jié)構(gòu) `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 ;
--
-- 導(dǎo)出表中的數(shù)據(jù) `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, '愛(ài)國(guó)', 'upfiles/CR-XyaJEndF1K.jpg');
復(fù)制代碼 代碼如下:
<?
$host="localhost"; //數(shù)據(jù)庫(kù)服務(wù)器名稱
$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>服務(wù)器連接失??!<br>請(qǐng)刷新后重試。</center>";
return true;
}
$databasename="database";//數(shù)據(jù)庫(kù)名稱
do
{
$con=mysql_select_db($databasename,$conn);
}while(!$con);
if ($con==FALSE)
{
echo "<center>打開(kāi)數(shù)據(jù)庫(kù)失??!<br>請(qǐng)刷新后重試。</center>";
return true;
}
?>
2.upload.php
復(fù)制代碼 代碼如下:
<?php
if ($_GET['action'] == "save"){
include_once('conn.php');
include_once('uploadclass.php');
$title=$_POST['title'];
$pic=$uploadfile;
if($title == "")
echo"<Script>window.alert('對(duì)不起!你輸入的信息不完整!');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>文件上傳實(shí)例</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>標(biāo)題:</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
復(fù)制代碼 代碼如下:
<?php
$uploaddir = "upfiles/";//設(shè)置文件保存目錄 注意包含/
$type=array("jpg","gif","bmp","jpeg","png");//設(shè)置允許上傳文件的類型
$patch="upload/";//程序所在路徑
//獲取文件后綴名函數(shù)
function fileext($filename)
{
return substr(strrchr($filename, '.'), 1);
}
//生成隨機(jī)文件名函數(shù)
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>";
}
//生成目標(biāo)文件的文件名
else{
$filename=explode(".",$_FILES['file']['name']);
do
{
$filename[0]=random(10); //設(shè)置隨機(jī)數(shù)長(zhǎng)度
$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
{//輸出圖片預(yù)覽
echo "<center>您的文件已經(jīng)上傳完畢 上傳圖片預(yù)覽: </center><br><center><img src='$uploadfile'></center>";
echo "<br><center><a href='upload.htm'>繼續(xù)上傳</a></center>";
}
}
}
?>
4.數(shù)據(jù)庫(kù)文件
-- phpMyAdmin SQL Dump
-- version 2.9.1.1
-- http://www.phpmyadmin.net
--
-- 主機(jī): localhost
-- 生成日期: 2009 年 07 月 30 日 21:43
-- 服務(wù)器版本: 5.0.11
-- PHP 版本: 5.1.1
--
-- 數(shù)據(jù)庫(kù): `database`
--
-- --------------------------------------------------------
--
-- 表的結(jié)構(gòu) `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 ;
--
-- 導(dǎo)出表中的數(shù)據(jù) `news`
--
INSERT INTO `news` (`id`, `title`, `content`, `path`) VALUES
(1, 'ag ', 'ag ', '1.html'),
(2, '你好2', '你好,新聞內(nèi)容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');
-- --------------------------------------------------------
--
-- 表的結(jié)構(gòu) `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 ;
--
-- 導(dǎo)出表中的數(shù)據(jù) `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, '愛(ài)國(guó)', 'upfiles/CR-XyaJEndF1K.jpg');
相關(guān)文章
PHP實(shí)現(xiàn)數(shù)組array轉(zhuǎn)換成xml的方法
這篇文章主要介紹了PHP實(shí)現(xiàn)數(shù)組array轉(zhuǎn)換成xml的方法,涉及php針對(duì)數(shù)組的遍歷及xml格式文件的構(gòu)造技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2016-07-07
c#中的實(shí)現(xiàn)php中的preg_replace
最近在按照一個(gè)php項(xiàng)目用c#重寫(xiě),一邊學(xué)習(xí)同時(shí)發(fā)現(xiàn)了他們的神似神不似的很多地方2009-12-12
php 數(shù)組的創(chuàng)建、調(diào)用和更新實(shí)現(xiàn)代碼
對(duì)于php的數(shù)組是php中很重要的一個(gè)地方,大家一定要仔細(xì)看。2009-03-03
PHP基于正則批量替換Img中src內(nèi)容實(shí)現(xiàn)獲取縮略圖的功能示例
這篇文章主要介紹了PHP基于正則批量替換Img中src內(nèi)容實(shí)現(xiàn)獲取縮略圖的功能,涉及php針對(duì)頁(yè)面img元素的正則匹配與替換操作相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2017-06-06
php動(dòng)態(tài)生成JavaScript代碼
生成javascript的實(shí)現(xiàn)代碼2009-03-03

