php实现图片、PDF、DOC、TXT多种文件上传下载功能(附源码)

 <!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>上传文件</title>
</head>
<body>
  <form action="{:U('Admin/Down/uploadfile')}" enctype="multipart/form-data" method="post" style="text-align:center;margin:30px;">
    <input type="file" name="photo"/>
    <input type="hidden" name="MAX_FILE_SIZE" value="204800" />
    <input type="hidden" name="type" value="webfile"/>
    <input type="submit" value="确定上传">
  </form>
</body>
</html>

//上传方法

 public function uploadfile(){
    $type=$_POST['type'];
    $chat=$_POST['phpto_chat'];
    $showname=$_POST['name'];
    $upload = new \Think\Upload();// 实例化上传类
    $upload->maxSize   =     3145728 ;// 设置附件上传大小
    $upload->exts      =     array('jpg', 'gif', 'png', 'jpeg','pdf','doc','docx','txt');
    // 设置附件上传类型
    $upload->rootPath  =     './Upload/file/'; // 设置附件上传根目录
    $upload->savePath  =     ''; // 设置附件上传(子)目录
    $upload->subName  = array('date','Ymd');
    $upload->saveName = array('uniqid','');
    // $upload->saveName = '';
    // 上传文件
    $info   =   $upload->upload();
    $data['pubtime'] = NOW_TIME;
    if(!$info) {// 上传错误提示错误信息
        $this->error($upload->getError());
    }else{// 上传成功
      // die(var_dump($type));
      $data['path'] = '/Upload/file/'.$info['photo']['savepath'].$info['photo']['savename'];
      $size=$info['photo']['size'];
      if($size>1024){
        $lastsize=intval($size/1024).'KB';
      }
      if(($size/1024)>1024){
        $lastsize=intval($size/1048576).'MB';
      }
      $downdata=array(
        'title'=>$info['photo']['name'],
        'type'=>$info['photo']['ext'],
        'size'=>$lastsize,
        'src'=>$data['path'],
        'time'=>time(),
      );
      $result = $this->downModle->add($downdata); //保存数据库
      if($result>0){
          $this->success("添加成功!", U('Admin/down/index'), 1);
      }else{
          $this->error("添加失败!", U('Admin/down/index'), 1);
      }
      $this->alertOpenerClose($data['path']);

    }
  }
//下载功能
    public function downfile(){
    if(isset($_GET['id'])){
      $data=$this->downModle->where('id='.$_GET['id'])->field('src,title')->select();
      $filename=$data[0]['src'];//获取文件的相对路径
      $title=$data[0]['title'];//获取文件的名称,必须带后缀格式
    }

   Header( "Content-type:  application/octet-stream ");
   Header( "Accept-Ranges:  bytes ");
   Header( "Accept-Length: " .filesize($filename));
   header( "Content-Disposition:  attachment;  filename= $title");//如果没带格式将生成不知道什么格式的文件
   echo file_get_contents('http://www.100txy'.$filename);//用绝对路径
   readfile($filename);
    }

在这里插入图片描述

  • 4
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
以下是一个示例HTML代码,可以实现发送doc/txt文件的在线聊天室: ```html <!DOCTYPE html> <html> <head> <title>在线聊天室</title> <meta charset="utf-8"> <script src="https://cdn.bootcss.com/jquery/3.4.0/jquery.min.js"></script> <style type="text/css"> .container { margin: 0 auto; width: 500px; height: 500px; border: 1px solid #ccc; overflow-y: scroll; padding: 10px; } .input-box { margin-top: 20px; } .file-input { display: none; } .btn { background-color: #4CAF50; color: white; padding: 12px 16px; border: none; cursor: pointer; width: 100%; margin-top: 10px; } .btn:hover { background-color: #3e8e41; } </style> </head> <body> <div class="container"></div> <div class="input-box"> <input type="text" id="input-text"> <button id="input-btn" class="btn">发送</button> <input type="file" id="input-file" class="file-input"> <button id="file-btn" class="btn">发送文件</button> </div> <script type="text/javascript"> var container = $(".container"); var inputText = $("#input-text"); var inputBtn = $("#input-btn"); var inputFile = $("#input-file"); var fileBtn = $("#file-btn"); function appendMessage(message) { container.append("<p>" + message + "</p>"); container.scrollTop(container[0].scrollHeight); } inputBtn.click(function() { var message = inputText.val(); appendMessage("我:" + message); inputText.val(""); }); fileBtn.click(function() { inputFile.click(); }); inputFile.change(function() { var file = this.files[0]; var reader = new FileReader(); reader.onload = function(e) { var data = e.target.result; appendMessage("我发送了一个文件:" + file.name); appendMessage('<a href="' + data + '" download="' + file.name + '">下载文件</a>'); }; reader.readAsDataURL(file); }); </script> </body> </html> ``` 在这个聊天室中,用户可以输入文本消息并发送,也可以点击“发送文件”按钮选择一个doc/txt文件并发送。当用户发送文件时,会在聊天室中显示一个链接,其他用户可以点击这个链接下载文件。需要注意的是,这个示例代码中只实现文件上传下载的基本功能,没有进行文件类型验证和安全性检查,开发者在实际使用时需要注意相关安全问题。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

付煜晨

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值