标签:
uploadify控件有一个坑爹的问题,就是文件上传时会session丢失,官方解释http://www.uploadify.com/documentation/uploadify/using-sessions-with-uploadify/,通过formData属性解决。
前端代码
<input type="file" name="file_upload" id="file_upload" />
$(‘#file_upload‘).uploadify({
‘auto‘ : false,//关闭自动上传
‘removeTimeout‘ : 1,//文件队列上传完成1秒后删除
‘swf‘ : basePath+‘resource/js/source/uploadify/uploadify.swf‘,
‘uploader‘ : basePath+‘home/file/fileUploadOper.html‘,
‘method‘ : ‘post‘,//方法,服务端可以用$_POST数组获取数据
‘buttonText‘ : ‘选择图片‘,//设置按钮文本
‘multi‘ : true,//允许同时上传多张图片
‘uploadLimit‘ : 10,//一次最多只允许上传10张图片
‘fileTypeDesc‘ : ‘Image Files‘,//只允许上传图像
‘fileTypeExts‘ : ‘*.gif; *.jpg; *.png; *.jpeg‘,//限制允许上传的图片后缀
‘fileSizeLimit‘ : ‘10MB‘,//限制上传的图片不得超过200KB
‘onUploadSuccess‘ : function(file, data, response){
data = data.substring(0,data.indexOf("}")+1);
var obj = JSON.parse(data);
if(obj.type){
alertWindow("info","信息",obj.msg);
}else{
alertWindow("error","错误",obj.msg);
}
},
‘onQueueComplete‘ : function(queueData) {}
});
$(‘#upload‘).click(function(){
var formData = {};
formData[‘did‘] = document.getElementById(‘did‘).value;
formData[‘session‘] = $(‘#session_id‘).val();
//$(‘#file_upload‘).uploadify(‘settings‘, ‘formData‘, {‘did‘:document.getElementById(‘did‘).value});
$(‘#file_upload‘).uploadify(‘settings‘, ‘formData‘, formData);
$(‘#file_upload‘).uploadify(‘upload‘,‘*‘);
});
$(‘#cancel‘).click(function(){
$(‘#file_upload‘).uploadify(‘cancel‘,‘*‘);
});
通过formData将sessionid传递到后台
后端代码
//接受动态传值
$did = $_POST[‘did‘];
session_id($_POST[‘session‘]);
session_start();
//获取目录
$dir = M(‘Dir‘);
$dirs = $dir->where("did=$did")->select();
if(count($dirs)>0){
$d = $dirs[0];
$realpath = $d[‘realpath‘];
//得到上传的临时文件流
$tempFile = $_FILES[‘Filedata‘][‘tmp_name‘];
//允许的文件后缀
//$fileTypes = array(‘jpg‘,‘jpeg‘,‘gif‘,‘png‘);
//得到文件原名
$fileName = $_FILES["Filedata"]["name"];
$names = explode(‘.‘,$fileName);
$newFlieName = create_guid().‘.‘.$names[1];
if(is_dir($realpath)){
if (move_uploaded_file($tempFile, $realpath.‘/‘.$newFlieName)){
//$user = session(‘user‘);
$user = $_SESSION[‘user‘];
//生成数据库数据
$file = M(‘File‘);
$file->filename = $fileName;
$file->realpath = $realpath.‘/‘.$newFlieName;
$file->realname = $newFlieName;
$file->path = $d[‘dpath‘].‘/‘.$newFlieName;
$file->did = $did;
$file->createuid = $user[‘uid‘];
$file->updateuid = $user[‘uid‘];
$file->createtime = time();
$file->updatetime = $file->createtime;
if($file->add()){
$result = ‘{"type":true,"msg":"‘.$fileName.‘上传成功。"}‘;
}else{
$result = ‘{"type":false,"msg":"‘.$fileName.‘上传失败,生成数据库记录时失败。"}‘;
}
}else{
$result = ‘{"type":false,"msg":"‘.$fileName.‘上传失败。"}‘;
}
}else{
$result = ‘{"type":false,"msg":"文档目录不存在,上传失败。"}‘;
}
}else{
$result = ‘{"type":false,"msg":"文档目录不存在,上传失败。"}‘;
}
echo $result;
使用session_id(),session_start()方法,后面通过$_FILES获取文件流进行文件写入操作。
ThinkPHP 3.2.3集成uploadify上传控件
标签:
原文地址:http://www.cnblogs.com/SR71BlackBird/p/5454608.html