1、下载
www.thinkphp.cn/down.html
www.uploadify.com/wp-content/uploads/files/uploadify.zip
2、TP3.2整合uploadify
在TP中放入这些文件
uploadify.css
jquery.uploadify.min.js
uploadify-cancel.png
uploadify.swf
引入
<link rel="stylesheet" type="text/css" href="__PUBLIC__/js/uploadify/uploadify.css" />
<tr>
<th>音频</th>
<td>
<input type="file" id="file_upload_audio" />
</td>
</tr>
<tr>
<th>视频</th>
<td>
<input type="file" name="smeta[video]" id="file_upload_video" />
</td>
</tr>
<tr>
<th>文件</th>
<td>
<input type="file" name="smeta[attachment]" id="file_upload_file" />
</td>
</tr>
<script type="text/javascript" src="__PUBLIC__/js/jquery.min.js"></script>
<script type="text/javascript" src="__PUBLIC__/js/uploadify/jquery.uploadify.min.js"></script>
//上传音频
$(function() {
$('#file_upload_audio').uploadify({
'swf' : '__PUBLIC__/js/uploadify/uploadify.swf',
'uploader' : '{:U("AdminPost/upload")}',
'buttonText': '上传音频',
//在浏览窗口底部的文件类型下拉菜单中显示的文本
'fileTypeDesc': 'Audio Files',
//允许上传的文件后缀
'fileTypeExts': '*.mp3; *.aac; *.flac; *.mar; *.wma; *.ape; *.ogg',
//发送给后台的其他参数通过formData指定
'formData': { 'type': 'audio'},
//选择文件后自动上传
'auto': true,
//设置为true将允许多文件上传
'multi': false,
//上传完成后执行
'onUploadSuccess': function(file, data, response){
eval("var data="+data);
console.log(data);
if(data.code == '1'){
$('#'+file.id).find('.data').html('上传成功');
$('#file_upload_audio').after('<audio src="'+data.data.file+'" controls="controls">您的浏览器不支持播放器</audio><input type="hidden" name="smeta[audio][]" value="'+data.data.file+'">');
}else{
$('#'+file.id).find('.data').html('上传失败,'+data.msg);
}
},
'onUploadError' : function(file, errorCode, errorMsg, errorString) {
alert('The file ' + file.name + ' could not be uploaded: ' + errorString);
},
});
});
//上传视频
$(function() {
$('#file_upload_video').uploadify({
'swf' : '__PUBLIC__/js/uploadify/uploadify.swf',
'uploader' : '{:U("AdminPost/upload")}',
'buttonText': '上传视频',
//在浏览窗口底部的文件类型下拉菜单中显示的文本
'fileTypeDesc': 'Video Files',
//允许上传的文件后缀
'fileTypeExts': '*.mp4; *.3gp; *.wmv; *.flv; *.mov; *.rm; *.rmvb',
//发送给后台的其他参数通过formData指定
'formData': { 'type': 'video'},
//选择文件后自动上传
'auto': true,
//设置为true将允许多文件上传
'multi': false,
//上传完成后执行
'onUploadSuccess': function(file, data, response){
eval("var data="+data);
console.log(data);
if(data.code == '1'){
$('#'+file.id).find('.data').html('上传成功');
$('#file_upload_video').after('<video src="'+data.data.file+'" controls="controls">您的浏览器不支持播放器</video><input type="hidden" name="smeta[video][]" value="'+data.data.file+'">');
}else{
$('#'+file.id).find('.data').html('上传失败,'+data.msg);
}
},
'onUploadError' : function(file, errorCode, errorMsg, errorString) {
alert('The file ' + file.name + ' could not be uploaded: ' + errorString);
},
});
});
//上传文件
$(function() {
$('#file_upload_file').uploadify({
// 'debug' : true,
'swf' : '__PUBLIC__/js/uploadify/uploadify.swf',
'uploader' : '{:U("AdminPost/upload")}',
'buttonText': '上传文件',
//发送给后台的其他参数通过formData指定
'formData': { 'type': 'file'},
//上传附件大小,0不限
'fileSizeLimit': '15MB',
//选择文件后自动上传
'auto': true,
//设置为true将允许多文件上传
'multi': true,
//允许上传文件个数
'uploadLimit' : 3,
'removeCompleted' : false,
//上传完成后执行
'onUploadSuccess': function(file, data, response){
eval("var data="+data);
console.log(data);
if(data.code == '1'){
$('#'+file.id).find('.data').html('上传成功');
// $('#file_upload_file').after('<a src="'+data.data.file+'">'+data.data.name+'</a><input type="hidden" name="smeta[attachment][]" value="'+data.data.file+'"><br>');
$('#file_upload_file').after('<input type="hidden" name="smeta[attachment][]" value="'+data.data.file+'"><br>');
}else{
$('#'+file.id).find('.data').html('上传失败,'+data.msg);
}
},
});
});
后端:
public function upload(){
//参数
$path = I('path','./data/upload/');
$type = I('type','file');
$isSaveName = I('isSaveName',true); //默认保存原始名称
$isReplace = I('isReplace',true); //默认替换同名文件
file_put_contents('post.log', var_export($_POST,true));
file_put_contents('files.log', var_export($_FILES,true));
//上传
$upload = new \Think\Upload();// 实例化上传类
$upload->maxSize = 15728640;// 15M 3145728 ;// 设置附件上传大小
// $upload->exts = array('mp3', 'mp4', 'gif', 'png', 'jpeg');// 设置附件上传类型
//根据类型放入对应的文件夹中
switch ($type) {
case 'image':
$upload->exts = array('jpg', 'bmp', 'gif', 'png', 'jpeg');// 设置附件上传类型
$upload->rootPath = './data/upload/image/'; // 设置附件上传根目录
break;
case 'audio':
$upload->exts = array('mp3', 'aac', 'flac', 'mar', 'wma', 'ape' , 'ogg');// 设置附件上传类型
$upload->rootPath = './data/upload/audio/'; // 设置附件上传根目录
break;
case 'video':
$upload->exts = array('3gp', 'mp4', 'wmv', 'flv', 'mov', 'rm', 'rmvb');// 设置附件上传类型
$upload->rootPath = './data/upload/video/'; // 设置附件上传根目录
break;
default:
$upload->rootPath = './data/upload/file/'; // 设置附件上传根目录
break;
}
$upload->savePath = ''; // 设置附件上传(子)目录
$upload->autoSub = true; // 自动使用子目录保存上传文件 默认为true
$upload->subName = array('date','Ymd'); //子目录命名
if($isSaveName){
$upload->saveName = ''; //保持上传文件名不变
}
$upload->replace = $isReplace; //存在同名文件是否是覆盖,默认为false
// 上传文件
$info = $upload->upload();
file_put_contents('upload.log', var_export($upload,true));
file_put_contents('info.log', var_export($info,true));
//返回上传结果
if(!$info) {// 上传错误提示错误信息
echo json_encode(array('code'=>'0','msg'=>$upload->getError()));
}else{// 上传成功
$arr = array(
'ext'=>$info['Filedata']['ext'],
'file'=>sp_get_host().'/data/upload/file/'.$info['Filedata']['savepath'].$info['Filedata']['savename'],
'size'=>$info['Filedata']['size'],
'type'=>$info['Filedata']['type'],
'name'=>$info['Filedata']['name'],
);
switch ($type) {
case 'image':
$arr['file'] = sp_get_host().'/data/upload/image/'.$info['Filedata']['savepath'].$info['Filedata']['savename'];
echo json_encode(array('code'=>'1','msg'=>'upload success','data'=>$arr));
break;
case 'audio':
$arr['file'] = sp_get_host().'/data/upload/audio/'.$info['Filedata']['savepath'].$info['Filedata']['savename'];
echo json_encode(array('code'=>'1','msg'=>'upload success','data'=>$arr));
break;
case 'video':
$arr['file'] = sp_get_host().'/data/upload/video/'.$info['Filedata']['savepath'].$info['Filedata']['savename'];
echo json_encode(array('code'=>'1','msg'=>'upload success','data'=>$arr));
break;
default:
echo json_encode(array('code'=>'1','msg'=>'upload success','data'=>$arr));
break;
}
}
}
===================
在上传中文文件的时候发现返回“文件上传保存错误”
这时候需要将TP/Think/Upload/Driver/Local.class.php文件中的83行改为 move_uploaded_file($file['tmp_name'],iconv("UTF-8","gb2312", $filename))