jQuery上传插件uploadify判断文件是否存在

4 篇文章 0 订阅
1 篇文章 0 订阅

最近因为工作需要,接触到了jQuery 的uploadify文件上传插件,上一篇转载的文章中有写它基本的用法,这里要说的问题是为了验证上传文件是否已经存在于服务器,就需要checkExisting这一选项,看页面JS代码如下:

'checkExisting' : '/static/js/plugins/uploadify/check-exists.php',

$(function(){
  //image upload
    $('#js_uploadModelPic').uploadify({   
      'formData': {
        'timestamp' : '{$timestamp}',
        'token'     : '{$token}',
        'folder'    : 'model-picture'
      }, 
      'swf':'/static/js/plugins/uploadify/uploadify.swf',
      'uploader':'/static/js/plugins/uploadify/uploadify.php',
      'fileTypeExts':'*.jpg; *.png; *.gif',
      'buttonText':'+', //按钮文字
      'buttonClass':'upload-add-btn', //按钮样式
      'removeCompleted' : false,
      'width':100, //按钮宽度
      'height':100, //按钮高度
      'auto':true, //自动上传
      'checkExisting' : '/static/js/plugins/uploadify/check-exists.php',
      'onUploadSuccess' : function(file, data, response) { 
        html = "<li><img src='"+data+"'' style='width:100px;height:100px;' class='thubm-imgs'></li><input type='hidden' name='model_media[]' value='"+data+"'>";       
        $("#js_imgHolder").append(html);
        $("#model_media").next("label").remove();
        $("#model_media").remove();
      }
    });
    
    
});

会交给check-exists.php后台文件来处理判断文件存不存在,最初的源文件是这样的:

$targetFolder = '/uploads'; // Relative to the root and should match the upload folder in the uploader script

if (file_exists($_SERVER['DOCUMENT_ROOT'] . $targetFolder . '/' . $_POST['filename'])) {
	echo 1;
} else {
	echo 0;
}
?>

就这么几句话,按常理来说,有了路径文件名是可以file_exists()判断文件是否存在服务器,但忽略了一个问题,就是如果我们自定义了上传文件存放的路径,如上所看到的floder:

 'formData': {
        'timestamp' : '{$timestamp}',
        'token'     : '{$token}',
        'folder'    : 'model-picture'
      },
若是这样的话,文件判断那就肯定是不存在的了,因为路径都不对了,肯定找不到该文件……,此时我们看到,既然文件名能够$_POST[]过来,那我们希望这个folder也能POST过来,那就来打印看看到底$_POST里面放了些什么,结果有些失望:

array (size=1)
  'filename' => string 'OOOPIC_SHIJUNHONG_20090809ad6104071d324dda.jpg' (length=46)
就只有filename,什么都没了,接着还是不甘心,对JS不咋样的我还是硬着头皮去看jquery.uploadify.min.js的源码,皇天不负有心人!找了好久终于找到了,相关代码如下:

onUploadStart: function(d) {
            var e = this.settings;
            var f = new Date();
            this.timer = f.getTime();
            this.bytesLoaded = 0;
            if (this.queueData.uploadQueue.length == 0) {
                this.queueData.uploadSize = d.size;
            }
            if (e.checkExisting) {
                c.ajax({
                    type: "POST",
                    async: false,
                    url: e.checkExisting,
                    data: {
                        filename: d.name
                    },
                    success: function(h) {
                        if (h == 1) {
                            var g = confirm('A file with the name "' + d.name + '" already exists on the server.\nWould you like to replace the existing file?');
                            if (!g) {
                                this.cancelUpload(d.id);
                                c("#" + d.id).remove();
                                if (this.queueData.uploadQueue.length > 0 && this.queueData.queueLength > 0) {
                                    if (this.queueData.uploadQueue[0] == "*") {
                                        this.startUpload();
                                    } else {
                                        this.startUpload(this.queueData.uploadQueue.shift());
                                    }
                                }
                            }
                        }
                    }
                });
            }
            if (e.onUploadStart) {
                e.onUploadStart.call(this, d);
            }
        },
原来这是已ajax提交过来的,其中的数据就只有filename,于是就想到把floder也传过去,修改:

data: {
                   filename: d.name,
                    floder: e.formData.folder
 },

然后再在check-exists.php文件接收:

// Define a destination
$targetFolder = '/upload/' . $_POST['floder'] . '/' ; // Relative to the root and should match the upload folder in the uploader script

if (file_exists($_SERVER['DOCUMENT_ROOT'] . $targetFolder . $_POST['filename'] )) {
    echo 1;
} else {
    echo 0;
}

这样就大功告成了,可以判断文件是否存在了!!

如下图:


然后也可以看到控制台这边的输出是1,说明该文件已经存在



  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值