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))