tp框架文件上傳

tp框架的文件上傳功能也很是簡便,咱們一塊兒來看一下:php

一、首先咱們要在Controller文件夾下新建一個控制器TestController.class.php:html

<?php
namespace Home\Controller;
use Think\Controller;
class TestController extends Controller{
    public function shangchuan (){
        $this->show();
    }
    public function wjsc(){
        $u = new\Think\Upload();
        $u->maxSize = 1024000;//設置文件大小
        $u->rootPath = "./Public/";//文件存放的根路徑
        $u->savePath = "./upload/";//設置當前上傳文件存放位置
        $u->exts = array('jpg','gif','png','jpeg');
        $info = $u->upload();//上傳文件並返回文件信息
        if($info){
            echo "上傳成功,文件存放在:".$info["file"]['savepath'];
        }
        else{
            echo $u->getError();
        }
    }
}

二、在View文件夾下新建一個Test文件夾,再在裏邊新建一個shangchuan.html頁面:框架

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>無標題文檔</title>
</head>

<body>
<h1>文件上傳</h1>
<form action="__CONTROLLER__/wjsc" method="post" enctype="multipart/form-data">
    <input type="file" name="file" />
    <input type="submit" value="上傳" />
</form>
</body>
</html>

效果圖:post

相關文章
相關標籤/搜索