最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
yii 图片上传及缩略图生成的程序
时间:2014-03-05 编辑:简简单单 来源:一聚教程网
Action文件:
代码如下 | 复制代码 |
/** * TestController.php * Created on: 2014-1-26 12:59:36 by Outsider */ class TestController extends CController { /** * 缩略图片生成 * @ path 图片路径 * @ width 图片宽度 * @ height 图片高度 */ public function actionGetThumb($path, $w, $h) { $file_name = md5($path . $w . $h); if (file_exists('./temp/' . $file_name . '.jpg')) { header('location:/temp/' . $file_name . '.jpg'); Yii::app()->end(); } Yii::import("ext.EPhpThumb.EPhpThumb"); $thumb = new EPhpThumb(); $thumb->init(); $thumb->create('.' . $path) ->adaptiveResize($w, $h) ->save('./temp/' . $file_name . '.jpg') ->show(); } /* * 图片显示 */ public function actionList() { $attache = Attache::model(); $list = $attache->findAll(); $this->render('list', array('list' => $list)); die; } /** * 文件上传 */ public function actionIndex() { $path = getcwd() . 'uploads'; $dir = DIRECTORY_SEPARATOR . date('Y') . DIRECTORY_SEPARATOR . date('m'); $dir = str_replace("\", "/", $dir); $uploads_dir = str_replace("\", "/", $path . $dir); if (!is_dir($uploads_dir) || !is_writeable($uploads_dir)) { mkdir($uploads_dir, 0777, TRUE); touch($uploads_dir . '/index.html'); } $uploaded = false; $model = new Upload(); if (isset($_POST['Upload'])) { $model->attributes = $_POST['Upload']; $file = CUploadedFile::getInstance($model, 'file'); $newName = substr(md5($file->extensionName . round((microtime(true) * 1000))), 0, 17) . '.' . $file->extensionName; // www.111com.net $file_name = $uploads_dir . '/' . $newName; if ($model->validate()) { $attache = new Attache(); $uploaded = $file->saveAs($file_name, TRUE); $attache->name = $file->getName(); $attache->path = $dir . '/' . $newName; $attache->create_time = time(); $attache->save(); } } $this->render('index', array( 'model' => $model, 'uploaded' => $uploaded, 'dir' => $uploads_dir, )); } } |
Upload.php:
代码如下 | 复制代码 |
|
图片显示页面:
自定义图片大小,缩略图自动生成
代码如下 | 复制代码 |
/** |
图片上传表单:
代码如下 | 复制代码 |
File was uploaded. Check . ('enctype'=>'multipart/form-data'))?> |
相关文章
- PHP导出数据超时的优化建议解读 10-31
- PHP之mysql位运算解析 10-31
- Laravel实现登录跳转功能解析 10-31
- php双向队列解读 10-31
- Laravel异常上下文解决教程 10-24
- php数组查询元素位置方法介绍 10-24