php登陆,文件上传

<?php 登陆页面
if($_POST){
    // 接受提交过来的数据
    $uname = trim($_POST['uname']);
    $pasd = md5($_POST['pasd']);
    
    // 查询数据库
    $conn = new mysqli('localhost','root','root','denglu');
    $sql = "select * from admin where uname = '$uname' and pasd = '$pasd' ";
    $res = $conn->query($sql);
    if($conn->error){
        die($conn->error);
    }
   $info = $res->fetch_assoc();
    // 判断数据
    if($res->num_rows > 0){
      
        setcookie('admin',$info['id']);这是设置cookie 括号内可以随便写
        header('Location:index.php');
    }else{
        $msg= '账号或者密码错误';
    }
    $conn->close();
}
?>
<?php 登陆后显示的页面
if(!isset($_COOKIE['admin'])){ 没有这个cookie值时就返回登陆页面
    header('Location:login.php');
}
$sid = $_COOKIE['admin'];
$conn = new mysqli('localhost','root','root','denglu');
$sql = "select * from admin where id = $sid "; 通过cookie值来获取是哪一个账号登陆的 通过id来找到
$res = $conn->query($sql);
$aa = $res->fetch_assoc(); $aa可以输出在登陆后名字显示的位置
$conn->close();
?>
<?php 退出登录
setcookie('admin',' ',time()-3600); 清除cookie
header('Location:login.php');然后跳回登陆页面

?>
--------------------------------------上传文件和获取文件----------------------
<?php
if($_FILES){
    // 读取文件后缀
    // var_dump($_FILES);
    //  把字符串根据.来分隔成数组获取文件的后缀名变为数组
    $temp = explode(".",$_FILES["file"]["name"]);
    //拿数组最后面的值也就是文件的后缀名
    $extension = end($temp);
    //判断文件类型 来限制上传
    if($_FILES["file"]["type"] = "image/jpg"||$_FILES["file"]["type"] = "image/png" && $_FILES["size"]<=204800){ 
        //新储存路径        这是随机生成的数字名称  拼接 上拿到的文件后缀名   
        $uname = '/upload/'.time().mt_rand(0,99).'.'.$extension;
        //这是把文件从系统内存中拿出来 存到指定位置
        move_uploaded_file($_FILES['file']['tmp_name'],'.'.$uname);
        // 存入数据库
        $conn = new mysqli('localhost','root','root','denglu');
        $sql = "insert into product (img) values('$uname')";
        $res = $conn->query($sql);
        var_dump($uname);
        $conn->close();
    }
}
//调取数据库中的图片
$connn = new mysqli('localhost','root','root','denglu');
$sqll = " select * from product where id =4";
$ress = $connn->query($sqll);
$r = $ress->fetch_assoc();
$connn->close();
?>
原文地址:https://www.cnblogs.com/sheep-fu/p/13055100.html