thinkphp 图片上传简单方法
1、在default中的Index文件夹中新建一个index.html模板,代码如下:
- <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
- <html>
- <head>
- <title> New Document </title>
- <meta name="Generator" content="EditPlus">
- <meta name="Author" content="">
- <meta name="Keywords" content="">
- <meta name="Description" content="">
- </head>
- <body>
- <form METHOD=POST action="__URL__/upload" enctype="multipart/form-data" >
-
- <input type="text" NAME="name" >
-
- <input type="text" NAME="email" >
-
- <input type="file" name="photo" >
-
- <input type="submit" value="保 存" >
-
- </form>
- </body>
- </html>
2、在控制器的IndexAction.class.php中执行下面代码
- <?php
-
- class IndexAction extends Action
- {
-
- public function index()
- {
- $this->display();
- }
- public function upload(){
- if(!emptyempty($_FILES)){
- $this->_upload();
- }
- }
- public function _upload(){
- import("ORG.Net.UploadFile");
- $upload = new UploadFile();
-
- $upload->maxsize = 3145728;
-
- $upload->allowExts = explode(',',"jpg,gif,jpeg,png");
-
- $upload->savePath = "./Tpl/default/Public/image/";
-
-
-
-
-
-
-
-
-
-
-
- $upload->saveRule = uniqid;
-
- $upload->thumbRemoveOrigin = true;
- if(!$upload->upload()){
-
- $this->error($upload->getErrorMsg());
- }else{
-
- $info = $upload->getUploadFileInfo();
- $this -> success("上传成功");
- }
-
- }
-
- }
- ?>