文件上传练习

练习一 :用户注册带头像

显示页面

<!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>
<form action="chuli.php" method="post" enctype="multipart/form-data">
<div>用户<input type="text" name="uid" /></div>
<div>密码<input type="text" name="pwd" /></div>
<div>姓名<input type="text" name="name" /></div>
<div>性别<input type="text" name="sex" /></div>
<div>生日<input type="text" name="birthday" /></div>
<div>头像<input type="file" name="pic" /></div>
<input type="submit" value="注册"  />
</form>
</body>
</html>

处理页面

<?php
var_dump($_FILES["pic"]);
include ("../DBDA.class.php");
$db=new DBDA();
//控制上传格式和大小
if(($_FILES["pic"]["type"]=="image/jpeg"||$_FILES["pic"]["type"]=="image/png")&& $_FILES["pic"]["size"]<=100000)
{
    //处理文件名
    $filename="./img/".time().$_FILES["pic"]["name"];
    //转编码格式
    $filename=iconv("UTF-8","gb2312",$filename);
    //判断文件是否存在
    if(!file_exists($filename))
    {
        //上传(保存)
        move_uploaded_file($_FILES["pic"]["tmp_name"],$filename);
    }
}
//向表user添加一条数据
$uid=$_POST["uid"];
$pwd=$_POST["pwd"];
$name=$_POST["name"];
$sex=$_POST["sex"];
$birthday=$_POST["birthday"];
$url="/lianxi/wenjianshangchuan/".$filename;
$sql="insert user values('','{$uid}','{$pwd}','{$name}','{$sex}','{$birthday}','{$url}')";
$result=$db->Query($sql,0);
if ($result)
{
    header("loaction:login.php");
}
else
{
    echo "添加失败";
}

练习二:文件下载

<!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>
<?php
include ("../DBDA.class.php");
$db=new DBDA();
$sql="select * from user ";
$attr=$db->Query($sql);
?>
<table border="1">
<tr>
<td>上传者</td>
<td>上传时间</td>
<td>文件名</td>
<td>操作</td>
</tr>
<?php
foreach($attr as $v)
{
    echo "
    <tr>
    <td>{$v[1]}</td>
    <td>{$v[5]}</td>
    <td>{$v[3]}</td>
    <td><a href='{$v[6]}'>下载</a></td>
    </tr>
    ";    
}


?>
原文地址:https://www.cnblogs.com/zoubizhici/p/5632505.html