ThinkPHP与PHP的上传与下载

来源:互联网 发布:java的历史 编辑:程序博客网 时间:2024/06/05 17:06

一、上传

1、thinkphp

<div id="content"><p>上传图片</p><form method="post" action="__CONTROLLER__/upload" enctype="multipart/form-data"><input class="input1" type="file"  name="file1"><input class="input2" type="submit" value="确定" /></form><if condition="$all['path'] neq ''"><br/><img src="__ROOT__{$all['path']}" height="100"><form action="__CONTROLLER__/download/id/{$all['id']}"><input type="submit" value="下载"/></form><else /><br/>暂无图片</if></div>

实现效果是:上传完图片直接显示出来
当前控制器下的upload方法:

    public function upload(){        $myblog=D('photo');        if (IS_POST){            if($_FILES['file1']['tmp_name']!=''){                $upload=new \Think\Upload();                $upload->maxSize   =     20000;//文件大小的最大值                $upload->exts      =     array('jpg',  'jpeg');//文件的类型                $upload->savePath  =      './Public/Uploads/';                $upload->rootPath  =      './';                $info   =   $upload->uploadOne($_FILES['file1']);                if(!$info){                    $this->error($upload->getError());                }else{                    $path=$info['savepath'].$info['savename'];                    $data['path']=$path;                    $data['name']=$info['savename'];                    $myblog->add($data);                    header("Location:".__CONTROLLER__."/index");//跳转,相当于刷新本页面                }            }        }    }

2、php

aa.php:

<html><body><form action="upl.php" method="post"enctype="multipart/form-data"><label for="file">上传:</label><input type="file" name="file" id="file" /> <br /><input type="submit" name="submit" value="确定" /></form><?php if(file_exists("upload/1.jpg")){    echo '<img src="upload/1.jpg"></img>';}?></body></html>

upl.php:

<?phpif (($_FILES["file"]["type"] == "image/jpg")||($_FILES["file"]["type"] == "image/jpeg")&& ($_FILES["file"]["size"] < 20000)){  if ($_FILES["file"]["error"] > 0){    echo "Return Code: " . $_FILES["file"]["error"] . "<br />";    }  else{    if (file_exists("upload/" . $_FILES["file"]["name"]))      {      echo $_FILES["file"]["name"] . " already exists. ";      }    else      {      move_uploaded_file($_FILES["file"]["tmp_name"],      "upload/" . $_FILES["file"]["name"]);      header("Location:aa.php");      }    }  }else  {  echo "Invalid file";  }?>

二、下载

1、thinkphp

<form action="__CONTROLLER__/download"><input type="submit" value="下载"/></form>

当前控制器的download方法:

public function download(){              $path="D:/WampServer/wamp64/www/a/myblog/Public/resume.txt";//文件的路径        $http=new \Org\Net\Http();        $http->download($path);    }

2、php

<form action="download.php"><input type="submit" value="下载"/></form>

download.php:

<?php/** * 文件下载 ***/header("Content-type:text/html;charset=utf-8");download('upload/resume.txt', 'resume.txt');function download($file, $down_name){//文件路径,文件名 //判断给定的文件存在与否  if(!file_exists($file)){  die("您要下载的文件已不存在,可能是被删除"); }  $fp = fopen($file,"r"); $file_size = filesize($file); //下载文件需要用到的头 header("Content-type: application/octet-stream"); header("Accept-Ranges: bytes"); header("Accept-Length:".$file_size); header("Content-Disposition: attachment; filename=".$down_name); $buffer = 1024; $file_count = 0; //向浏览器返回数据  while(!feof($fp) && $file_count < $file_size){  $file_con = fread($fp,$buffer);  $file_count += $buffer;  echo $file_con; }  fclose($fp);}?>
0 0
原创粉丝点击