老师下载文件怎么实现,出现乱码了

老师下载文件怎么实现,出现乱码了

<?php
require_once './lib/fun.php';
error_reporting(E_ALL^E_NOTICE^E_WARNING);
header('content-type:text/html;charset=utf-8');


$dsn = "mysql:host=localhost;dbname=king";
$pdo = new PDO($dsn,'root','123456');
$pdo->exec('set names utf8');


$upload=$_FILES['upload']['tmp_name'];
// var_dump($upload);
$info=$_POST['info'];
//图片和描述必须要存在;
if ($info!=''&&$upload!='') {
//先插入数据
  $sql2 = "INSERT INTO uploaddemo(info) values('$info') ";

  $stmt = $pdo->prepare($sql2);

  $result = $stmt->execute();
//然后查询数据,把ID值赋给图片名称
  $sql = "SELECT * FROM  uploaddemo order by id desc ";
  $stmt = $pdo->prepare($sql);

  $result = $stmt->execute();


  $data = $stmt->fetchAll(PDO::FETCH_ASSOC);
  $upload = new Upload();
  //吧ID值传参给上传函数
  $upload->handleUploadFile($data[0]['id']);

}



?>
<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <!-- 上述3个meta标签*必须*放在最前面,任何其他内容都*必须*跟随其后! -->
    <title>文件上传和下载</title>
    <!-- Bootstrap -->
    <link href="style/css/bootstrap.min.css" rel="stylesheet">
    <link href="style/css/site.min.css" rel="stylesheet">
    <style>
        .projects .thumbnail .caption {
            height: auto;
            max-width: auto;
        }
        .image{
            margin:10px auto;
            border-radius:5px;
            overflow:hidden;
            border:1px solid #CCC;
        }
        .image .caption P{
            text-align: center;
        }
    </style>
</head>

<body>
    <!--导航栏-->
    <div class="navbar navbar-inverse navbar-fixed-top">
        <div class="container">
            <div class="navbar-header">
                <button class="navbar-toggle collapsed" type="button" data-toggle="collapse" data-target=".navbar-collapse">
                  <span class="sr-only">Toggle navigation</span>
                  <span class="icon-bar"></span>
                  <span class="icon-bar"></span>
                  <span class="icon-bar"></span>
                </button>
                <a class="navbar-brand hidden-sm" href="index.php" onclick="_hmt.push(['_trackEvent', 'navbar', 'click', 'navbar-首页'])">慕课网</a>
            </div>
            <div class="navbar-collapse collapse" role="navigation">
                <ul class="nav navbar-nav">
                    <li class="hidden-sm hidden-md">
                        <a href="" target="_blank"></a>
                    </li>
                    <li>
                        <a href="" target="_blank"></a>
                    </li>
                </ul>
            </div>
        </div>
    </div>
    <!--导航栏结束-->
    <!--巨幕-->
    <div class="jumbotron masthead">
        <div class="container">
            <h1>文件上传下载</h1>
            <h2>实现文件的上传和下载功能</h2>
            <p class="masthead-button-links">
                <form class="form-inline" action="index.php" method="get">
                    <div class="form-group">
                        <input type="text" class="form-control" id="exampleInputName2" placeholder="输入搜索内容" name="key" value="">
                        <button class="btn btn-default" type="submit">搜索</button>
                        <button type="button" class="btn btn-primary btn-default" data-toggle="modal" data-target="#myModal">  上传  </button>
                    </div>
                </form>
            </p>
        </div>
    </div>
    <!--巨幕结束-->
    <!-- 模态框 -->
    <div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
        <div class="modal-dialog" role="document">
            <form class="form-inline" action="index.php" method="post" enctype="multipart/form-data">
                <div class="modal-content">
                    <div class="modal-header">
                        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                        <h4 class="modal-title" id="myModalLabel">上传图片</h4>
                    </div>
                    <div class="modal-body">
                        <p>选择图片:</p><input type="file" id="image" name="upload">
                        <br/>
                        <p>图片描述:</p><textarea class="form-control" cols="75" name="info" id="info"></textarea>
                        <br/><br/>
                        <p>
                          是否添加水印:
                          <select name="mark">
                            <option value="1">添加</option>
                            <option value="0">不添加</option>
                          </select>
                        </p>
                        <br/>
                        <p>
                          图片宽度比例:
                            <select name="scale">
                              <option value="800*600">800*600</option>
                              <option value="600*450">600*450</option>
                              <option value="400*300">400*300</option>
                            </select>
                        </p>
                    </div>
                    <div class="modal-footer">
                        <button type="submit" class="btn btn-default" name="submit" id="submit" onclick="show(this)">上传</button>
                        <button type="reset" class="btn btn-primary">重置</button>
                    </div>
                </div>
            </form>
        </div>
    </div>
    <!--模态框结束-->

    <div class="container projects">
        <div class="projects-header page-header">
            <h2>上传图片展示</h2>
            <p>将上传的图片展示在页面中</p>
        </div>
        <div class="row">
          <?php

            //把数据库内容遍历出来
          $sql = "SELECT * FROM  uploaddemo order by id desc ";
          $stmt = $pdo->prepare($sql);

          $result = $stmt->execute();
          $data = $stmt->fetchAll(PDO::FETCH_ASSOC);

           foreach ($data as $key => $value) {
             $username=(int)$value['id'];
           ?>
            <div class="col-sm-6 col-md-3 col-lg-4 ">
                <div class="image">                        <!--把函数中的得到的图片名在此使用 -->
                    <a href="index.php?filename=<?php echo  $username.'.jpg'  ?>"  class="btn btn-lg btn-success"  ><img class="img-responsive" src="<?php echo  './upload/'.($username).'.'.pathinfo($info['name'],PATHINFO_EXTENSION) ?>"></a>
                    <div class="caption">
                      <p>
                        <?php echo $value['info']; ?>
                      </p>
                    </div>
                </div>
            </div>
          <?php
          $filename= $_REQUEST['filename'];
          $filepath = './upload/'.$filename;

          if(!file_exists($filepath)) {
              exit('文件不存在');
          } else {
              header("Content-type: application/octet-stream");
              header('Content-Disposition: attachment; filename='  .basename($filepath));
              readfile($filepath);
          }

} ?>

        </div>
        <!--分页-->
        <nav aria-label="Page navigation" style="text-align:center">
            <ul class="pagination pagination-lg">
                <li>
                    <a href="" aria-label="Previous">
                        <span aria-hidden="true">首页</span>
                    </a>
                </li>
                <li><a href="">1</a></li>
                <li><a href="">2</a></li>
                <li><a href="">3</a></li>
                <li><a href="">4</a></li>
                <li><a href="">5</a></li>
                <li>
                    <a href="" aria-label="Next">
                        <span aria-hidden="true">尾页</span>
                    </a>
                </li>
            </ul>
        </nav>
    </div>

    <footer class="footer  container">
        <div class="row footer-bottom">
            <ul class="list-inline text-center">
                <h4><a href="class.imooc.com" target="_blank">class.imooc.com</a> | 慕课网</h4>
            </ul>
        </div>
    </footer>

    <!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
    <script src="style/js/jquery.min.js"></script>
    <!-- Include all compiled plugins (below), or include individual files as needed -->
    <script src="style/js/bootstrap.min.js"></script>
    <script type="text/JavaScript">
        function show(){
          if(document.getElementById("image").value == ''){
            alert('请选择图片');
          }
          if(document.getElementById("info").value == ''){
            alert('请输入图片描述');
          }
        }
    </script>
</body>

</html>

正在回答

登陆购买课程后可参与讨论,去登陆

1回答

您好,您的下载是在此页面操作的吗?小慕没有找到下载的按钮呢。乱码是指什么时候出现的乱码呢?建议同学提交作业,将相关页面以及数据库全部提交,只有单个页面,没有相关代码和数据库,小慕也是无法测试呢。祝学习愉快!

  • 愿者__上钩 提问者 #1
    对,就是在此页面操作,用a标签点击图片下载
    2018-05-02 22:20:25
  • imooc_澈 回复 提问者 愿者__上钩 #2
    您好,您的作业老师已经进行了批改,并就相关内容给出了具体建议,您可以下载文档进行查看,祝学习愉快~
    2018-05-03 14:41:11
问题已解决,确定采纳
还有疑问,暂不采纳

恭喜解决一个难题,获得1积分~

来为老师/同学的回答评分吧

0 星
请稍等 ...
意见反馈 帮助中心 APP下载
官方微信

在线咨询

领取优惠

免费试听

领取大纲

扫描二维码,添加
你的专属老师