<!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','');
$info = $upload->upload();
$data['pubtime'] = NOW_TIME;
if(!$info) {
$this->error($upload->getError());
}else{
$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);
}
|