php 上传文件实例 上传并下载word文件

时间:2021-10-15 07:21:57

上传界面

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
</head> <body>
<h1>上传文件</h1>
<div>说明:只能上传word文档,上传文件的大小必须小于1M</div>
<br />
<form action="wenjianchuli5.php" method="post" enctype="multipart/form-data">
<input type="file" name="file" />
<input type="submit" value="上传" />
</form>
</body>
</html>

显示并处理界面

<?php
session_start();
$_SESSION["uid"] = "lisi"; include("DBDA.php");
$db = new DBDA(); var_dump($_FILES); $filename=""; //加限制条件
if($_FILES["file"]["type"]=="application/octet-stream" && $_FILES["file"]["size"]<1048576)
{
//造存储路径
$filename="./img/".date("YmdHis").$_FILES["file"]["name"];
//转编码格式
$filename = iconv("utf-8","gb2312",$filename); //文件是否存在
if(!file_exists($filename))
{
//移动文件保存
move_uploaded_file($_FILES["file"]["tmp_name"],$filename);
}
} //向文件表添加一条数据
$uid = $_SESSION["uid"];
$lujing = iconv("gb2312","utf-8",$filename);
$urls = "/".$lujing;
$times = date("Y-m-d H:i:s");
$name = $_FILES["file"]["name"]; $sql = "insert into wenjian values('','{$uid}','{$urls}','{$times}','{$name}')"; $db->Query($sql,0); header("location:wenjianshangchuan6.php");

显示界面

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
</head>
<?php
session_start();
include("DBDA.php");
$db = new DBDA(); $uid = $_SESSION["uid"]; $sql = "select * from wenjian where uid='{$uid}'";
$attr = $db->Query($sql);
?>
<body>
<h1>文件管理页面</h1>
<table width="100%" border="1" cellpadding="0" cellspacing="0">
<tr>
<td>上传者</td>
<td>上传时间</td>
<td>文件名</td>
<td>操作</td>
</tr>
<?php
foreach($attr as $v)
{
$sname = "select name from users where uid='{$v[1]}'";
$name = $db->StrQuery($sname);
echo "<tr>
<td>{$name}</td>
<td>{$v[3]}</td>
<td>{$v[4]}</td>
<td><a href='{$v[2]}'>下载</a></td>
</tr>";
}
?>
</table>
</body>
</html>