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 (! empty empty ( $_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->thumb = true; //设置引用图片类库包路径 //$upload->imageClassPath = 'ORG.Net.Image'; //设置需要生成缩略图他的文件后缀 //$upload->thumbPrefix ='m_,s_'; //生成2张缩略图 //设置缩略图最大宽度 //$upload->thumbMaxWidth = '400,100'; //设置缩略图最大高度 //$upload->thumbMaxHeight = '400,100'; //设置上传文件规则 $upload ->saveRule = uniqid; //删除原图 $upload ->thumbRemoveOrigin = true; if (! $upload ->upload()){ //捕获上传异常 $this ->error( $upload ->getErrorMsg()); } else { //取得成功上传文件信息 $info = $upload ->getUploadFileInfo(); $this -> success( "上传成功" ); } } } ?>查看更多关于thinkphp 图片上传简单方法 - Thinkphp的详细内容...
声明:本文来自网络,不代表【好得很程序员自学网】立场,转载请注明出处:http://www.haodehen.cn/did6363