磨磨唧唧几天终于把这个东西写出来了,也算一个小案例吧.新手不容易啊,是吧!!这里由衷感谢, phpnewnew,老师.
上代码吧.
上传页面,upload.html
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>文件上传</title>
</head>
<body>
<div align="center">
<h1>文件上传</h1><br/>
<img src="http://i.imgur.com/I0CIv.jpg" title="来自www.blue7wings.com" alt="www.blue7wings.com" />
<img src="F:/apache/htdocs/php/23.jpg"/>
<form action="upload.php" method="post" enctype="multipart/form-data">
<input type="hidden" name="max_file_size" value="10000000"/>
<h2>文件名:</h2>
<input type="text" name="filename" /><br/>
<h2>简介:</h2>
<textarea name="intro" cols="30px" rows="20px"></textarea>
<h2>选择文件:</h2>
<input type="file" name="userfile" id="userfile"/>
<input type="submit" value="提交"/>
<input type="reset" value="重置"/>
</form>
<a href="fileshow.php">查看上传文件</a>
</div>
</body>
上传处理页面,upload.php
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<?php
//发送用户信息
$filename=$_REQUEST['filename'];
$intro=$_REQUEST['intro'];
//判断是否上传成功
if($_FILES['userfile']['error']>0)
echo "上传失败!!<br/>";
//判断上传的类型
if($_FILES['userfile']['type'] !='image/jpeg'&&$_FILES['userfile']['type'] !='image/gif')
{
echo "请选择正确的文件类型!!<br/>";exit();
}
$path=$_FILES['userfile']['name'];
$path=iconv("utf-8","gb2312",$path);//防止乱码问题的出现
if(is_uploaded_file($_FILES['userfile']['tmp_name']))
{
if(!move_uploaded_file($_FILES['userfile']['tmp_name'],$path))
{
echo "文件移动失败";exit();
}
}
//将文件信信息写入数据库
@$conn=new mysqli("localhost","root","123456","php");
if(!$conn)
exit("数据库连接失败!!");
$query1="insert into file (fileName,fileIntro,fileLoc) values('{$filename}','{$intro}','$path')";
$result=$conn->query($query1) ;
if(!$result)
echo "文件上传失败";
else
if($conn->affected_rows)
echo "文件上传成功!!";
else
echo "文件上传失败!!";
?>
<html>
<title>以上传文件</title>
<body>
<a href="http://localhost/php/fileshow.php">查看以上传图片:</a>
</body>
</html>
文件共享界面,fileshow.php
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<?php
@$conn=new mysqli("localhost","root","123456","php");
if(!$conn)
die("数据库查询失败");
$query1="select * from file ";
$result=$conn->query($query1);
if(!$result)
die("获取结果失败");
while($res=$result->fetch_row())
{
echo "<h2>$res[0]</h2><br/>";
echo "<font size=5>文件名:</font>$res[1]<br/>";
echo "<font size=5>简介:</font>$res[2]<br/>";
echo "<img src='$res[3]'/><br/><br/>";
}
echo "<a href='upload.html'>继续上传</a>";
?>
mysql数据库:
create table file(
id int primary key auto_increment,
fileName varchar(50),
fileIntro varchar(100),
fileLoc varchar(100)
)
这是很简陋的,是一个简易的版本,但是对我是个很大的意义.加油,以后肯定会写出更好的代码的...