swfupload相关问题[PHP]
6、在弹出层或者一个页面多个swfupload时,会出现swfupload_0重复,导致新的swfflash无法创建
var swfuOptionEdit = {//swfupload选项movieName : 'swfupload_editalbum_0',
id : 'swfupload_editalbum_0',
swfupload_loaded_handler: swfUploaderLoaded2
}
function swfUploaderLoaded2() {
//swfuEdit.refreshCookies();
$('#_uploadandshowimg').children().get(0).id = "_album_edit";
}
也就是当前加载完改自己的id,这样就能保证不会出现swfupload_0重复的现象。
5、方维中使用swfupload解决flash发送IE cookie的方案,目前在多个页面使用,未发现异常
PHP端代码:special_cookie();
function special_cookie(){
//请勿修改这里的内容,造成上传失败
if(!empty($_POST["saltkey"])){
global $config;
include ROOT_PATH . '/public/config.global.php';
$cookie_pre = $config['cookie']['cookie_pre'].substr(md5($config['cookie']['cookie_path'].'|'.$config['cookie']['cookie_domain']), 0, 4).'_';
setcookie ('saltkey', $_POST['saltkey']);
setcookie ('sid', $_POST['sid']);
setcookie ('auth', $_POST['auth']);
setcookie ($cookie_pre . 'auth', $_POST['auth']);
setcookie ($cookie_pre . 'sid', $_POST['sid']);
setcookie ($cookie_pre . 'saltkey', $_POST['saltkey']);
//unset($config);
}
}
swfupload中配置中必须含有:
post_params : {
"auth" : auth,//取得的cookie auth
"saltkey" : '<!--{$_FANWE["cookie"]["saltkey"]}-->',
"sid" : sid,////取得的cookie sid
"albumid" : "<!--{$id}-->"
},
这样就能保证在上传图片时为同一用户,似乎swfupload的swfupload.cookie.js没起作用一样。
在方维中,只要能保证sid, auth, saltkey就能保证用户唯一。
4、上面的“2、swfupload丢失cookie”有些局限性
通过swfu.refreshCookies(true),是可以把cookie带过去的,但说法不准确,准确的说法是:将IE cookie带过去了。对于非IE浏览器,某些登录验证信息还是要通过post_param传过去的。而且官方也说了:Cookie issue
On Windows the Non-IE Flash Player plugin (FireFox, Opera, Safari, etc) will send the IE cookies regardless of the browser used. This breaks authentication and sessions for many server-side scripting technologies.
在windows上的非IE flash插件将发送IE cookies。
Developers should manually pass Session and Authentication cookie information and manually restore Sessions on the Server Side if they wish to use Sessions
The SWFUpload package contains work-around sample code for PHP and ASP.Net
SWFUpload 代码示例包中已经含有PHP和ASP.Net的解决方案。
解决方案:必须将PHP端的验证登录信息POST到相应的PHP文件上才行。
这也是我最近开发方维时抓包发现的,汗。
PS:方维主要是验证auth,但同时要保证authkey一致才行,要想保证authkey一致,必须将saltkey用swfupload传过来,重新写入cookie,PHP才能正常自动登录验证并上传图片。鄙视一下自己,做PHP多年,才知道,用函数写cookie可以当时写当时取。
示例:方维swfupload上传图片
PHP端用于写入由swfupload传来的cookie:
if(!empty($_POST["saltkey"])){
include ROOT_PATH . '/public/config.global.php';
$cookie_pre = $config['cookie']['cookie_pre'].substr(md5($config['cookie']['cookie_path'].'|'.$config['cookie']['cookie_domain']), 0, 4).'_';
setcookie ('saltkey', $_POST['saltkey']);
setcookie ('sid', $_POST['sid']);
setcookie ('auth', $_POST['auth']);
setcookie ($cookie_pre . 'auth', $_POST['auth']);
}
swfupload中配置文件里加上:
"auth" : auth,
"saltkey": '<!--{$_FANWE["cookie"]["saltkey"]}-->',
"sid" : sid,
saltkey是无法直接用JS获取的,因为它是http_only式的cookie,只能用服务器端脚本获取。
PS:经多方面检查,上面的必须写成函数然后调用才能即时读写cookie成功,切记切记。
3、Call to SetPostParams failed错误
google了一下,http://stackoverflow.com/questions/9494107/swfupload-issue-cant-use-its-method-addpostparam说是swfupload没有加载完。如果把对swfupload的任何操作放在window.onload里也行,不过我不喜欢。
既然是对swfuoload进行相关操作,可以当swfupload加载完后再进行操作也行的呀,看招:
var settings = {
//用来刷新cookie的
// Your settings...
// More settings...
// Some more...
swfupload_loaded_handler: swfUploaderLoaded
}
swfu = new SWFUpload(settings);
function swfUploaderLoaded() {
// Loaded! Call it now.
this.addPostParam("user_id", "123456789"); // or swfu.addPostParam()
swfu.refreshCookies();}
swfupload_loaded_handler:当swfupload加载完成后执行的句柄操作或处理。
2、swfupload 丢失cookie状态(仅适用于IE):
如果需要swfupload把cookie也传过来的话,需要加入:swfupload = new SWFUpload( settings ); //在新建swfupload上传对象后刷新cookie至flash参数 swfupload.refreshCookies(); //关键是这一句起主导作用,其它啥的都是无用的,至少在PHP上是这样的。如果不刷新cookie的话,后台会丢失登录状态,导致图片ajax上传失败
这里也有一篇:http://www.shirne.com/?cid=17&id=145
此方法不完美,因为http_only的cookie,JS是取不到值的,自然也传不过去。
1、swfupload 简单使用:
<script src="{TPL_PATH}/swfupload/swfupload.js"></script><script src="{TPL_PATH}/swfupload/plugins/swfupload.cookies.js"></script>
<div id="swfu-placeholder">
</div>
<script> function fileDialogComplete(selectedNum, queuedNum) { if (queuedNum > 0) {//选择并添加到上传队列的文件数大于0 this.startUpload();//开始上传 this.setButtonDisabled(true);//禁用上传按钮 } } //上传过程中出错 function uploadError(file, errCode, msg) { var cc = ['文件[', file.name, ']上传出错,出错代码:[', errCode, '],出错原因:', msg].join(''); alert(cc); } //上传成功 function uploadSuccess(file, data) { var cc = ['文件[', file.name, ']上传成功,服务器返回信息:', data].join(''); alert(cc); } //上传完成,无论上传过程中出错还是上传成功,都会触发该事件,并且在那两个事件后被触发 function uploadComplete(file) { var cc = ['文件[', file.name, ']结束上传'].join(''); this.setButtonDisabled(true);//恢复上传按钮 this.button_image_url = file.name; } var swfuOption = {//swfupload选项 upload_url : "{u album/save}", //接收上传的服务端url flash_url : "{TPL_PATH}/swfupload/swfupload.swf",//swfupload压缩包解压后swfupload.swf的url button_placeholder_id : "swfu-placeholder",//上传按钮占位符的id file_size_limit : "1024",//用户可以选择的文件大小,有效的单位有B、KB、MB、GB,若无单位默认为KB button_width: 310, //按钮宽度 button_height:300, //按钮高度 button_text: '',//按钮文字 button_cursor : SWFUpload.CURSOR.HAND, button_image_url: './tpl/images/upload.jpg', button_action: SWFUpload.BUTTON_ACTION.SELECT_FILE, file_post_name : "upfile", post_params : { "post_param_name_1" : "post_param_value_1", "post_param_name_2" : "post_param_value_2" }, file_types : "*.jpg;*.gif;*.png;*.jpeg", file_types_description: "Web Image Files", file_dialog_complete_handler: fileDialogComplete, upload_error_handler: uploadError,//文件上传出错 upload_success_handler: uploadSuccess,//文件上传成功 upload_complete_handler: uploadComplete//文件上传完成,在upload_error_handler或者upload_success_handler之后触发 } var swfu = new SWFUpload(swfuOption);//初始化并将swfupload按钮替换swfupload占位符 </script>
效果:
后台PHP:
print_r($_FILES);exit;
- 没有相关文章
- 没有评论