基于thinkphp的uploadify上传图功能
php Action server端
<?php
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
* Description of UploadAction
*
* @author hxwj
*/
class UploadAction extends CommonAction{
public function _initialize(){
//此处为解决Uploadify在火狐下出现http 302错误 又一次设置SESSION
$session_name = session_name();
if (isset($_POST[$session_name])) {
session_id($_POST[$session_name]);
session_start();
}
}
public function upload(){
import("ORG.Net.UploadFile");
//导入上传类
if($_FILES){
$upload = new UploadFile();
//设置上传文件大小
$upload->maxSize = 204800;
//设置上传文件类型
$upload->allowExts = array('jpg', 'gif', 'png', 'jpeg');
//设置附件上传文件夹
$upload->thumb = true;
$upload->thumbPrefix = 'm_'; //生产2张缩略图
//设置缩略图最大宽度
$upload->thumbMaxWidth = '400,100';
//设置缩略图最大高度
$upload->thumbMaxHeight = '400,100';
$upload->savePath = './Uploads/shunongjj/';
//设置须要生成缩略图,仅对图像文件有效
// 设置引用图片类库包路径
//删除原图
//$upload->thumbRemoveOrigin = true;
if (!$upload->upload()) {
//捕获上传异常
//$this->error($upload->getErrorMsg());
$data['status']='0';
$this->ajaxReturn($data,'json');
} else {
//取得成功上传的文件信息
$info = $upload->getUploadFileInfo();
$imgpath = '/Uploads/shunongjj/'.$info[0]['savename'];
$data['picurl'] = $imgpath;
$data['status']='100';
$this->ajaxReturn($data,'json');
}
}
}
/*
* 无缩略图
*/
public function upload_wsl(){
import("ORG.Net.UploadFile");
//导入上传类
if($_FILES){
$upload = new UploadFile();
//设置上传文件大小
$upload->maxSize = 204800;
//设置上传文件类型
$upload->allowExts = array('jpg', 'gif', 'png', 'jpeg');
//设置附件上传文件夹
$upload->savePath = './Uploads/shunongjj/';
//设置须要生成缩略图,仅对图像文件有效
// 设置引用图片类库包路径
//删除原图
//$upload->thumbRemoveOrigin = true;
if (!$upload->upload()) {
//捕获上传异常
//$this->error($upload->getErrorMsg());
$data['status']='0';
return false;
// $this->ajaxReturn("","上传失败","");
} else {
//取得成功上传的文件信息
$info = $upload->getUploadFileInfo();
return $imgpath = '/Uploads/shunongjj/'.$info[0]['savename'];
}
}
}
/*
* 公司轮换首页图片
*/
public function upload_index(){
import("ORG.Net.UploadFile");
//导入上传类
if($_FILES){
$upload = new UploadFile();
//设置上传文件大小
$upload->maxSize = 204800;
//设置上传文件类型
$upload->allowExts = array('jpg', 'gif', 'png', 'jpeg');
//设置附件上传文件夹
$upload->savePath = './Uploads/shunongjj/';
//设置须要生成缩略图,仅对图像文件有效
// 设置引用图片类库包路径
//删除原图
//$upload->thumbRemoveOrigin = true;
if (!$upload->upload()) {
//捕获上传异常
//$this->error($upload->getErrorMsg());
$data['status']='0';
$this->ajaxReturn($data,'json');
} else {
//取得成功上传的文件信息
$info = $upload->getUploadFileInfo();
$imgpath = '/Uploads/shunongjj/'.$info[0]['savename'];
$data['picurl'] = $imgpath;
$data['status']='100';
$this->ajaxReturn($data,'json');
}
}
}
}
?>
模板 tpl 端
html
<input id="file_upload" name="file_upload" type="file" />(图片上传规范:176*107)
js
<script src="__PUBLIC__/uploadify/jquery.uploadify.min.js" type="text/javascript"></script>
<link rel="stylesheet" type="text/css" href="__PUBLIC__/uploadify/uploadify.css"/>
/**
* uploadify插件上产图片
*/
$("#file_upload").uploadify({
//指定swf文件
'swf': '__PUBLIC__/uploadify/uploadify.swf',
//后台处理的页面
'uploader': '{:U("Upload/upload")}',
//button显示的文字
'buttonText': '上传图片',
//显示的高度和宽度,默认 height 30;width 120
//'height': 15,
//'width': 80,
//上传文件的类型 默觉得全部文件 'All Files' ; '*.*'
//在浏览窗体底部的文件类型下拉菜单中显示的文本
'fileTypeDesc': 'Image Files',
//同意上传的文件后缀
'fileTypeExts': '*.gif; *.jpg; *.png',
//发送给后台的其它參数通过formData指定
//'formData': { 'someKey': 'someValue', 'someOtherKey': 1 },
//上传文件页面中,你想要用来作为文件队列的元素的id, 默觉得false 自己主动生成, 不带#
//'queueID': 'fileQueue',
//选择文件后自己主动上传
'auto': true,
//设置为true将同意多文件上传
'onUploadSuccess': function(file, data, response){
var data = $.parseJSON(data);
if(data.status==100) {
$("#img_show").html("<img src='"+data.picurl+"'/>");
$("#img_url").val(data.picurl);
}else{
alert("上传失败,可能是您上传的图片大小不符合规范,请重试!");
}
}
});
});