溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

php如何上傳圖片到指定位置路徑保存到數據庫

發布時間:2021-09-29 17:39:41 來源:億速云 閱讀:193 作者:小新 欄目:開發技術

這篇文章主要介紹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">&nbsp;</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>&nbsp;</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');

以上是“php如何上傳圖片到指定位置路徑保存到數據庫”這篇文章的所有內容,感謝各位的閱讀!希望分享的內容對大家有幫助,更多相關知識,歡迎關注億速云行業資訊頻道!

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

php
AI

亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女